Skip to content

Commit

Permalink
Add ERROR_CODES enum for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
guidomodarelli committed Oct 15, 2024
1 parent a71486b commit fcd0024
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions plugins/main/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,8 @@ export const SEARCH_BAR_DEBOUNCE_UPDATE_TIME = 400;

// ID used to refer the createOsdUrlStateStorage state
export const OSD_URL_STATE_STORAGE_ID = 'state:storeInSessionStorage';

export enum ERROR_CODES {
EPROTO = 'EPROTO',
ECONNREFUSED = 'ECONNREFUSED',
};
9 changes: 5 additions & 4 deletions plugins/main/server/controllers/wazuh-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { KeyEquivalence } from '../../common/csv-key-equivalence';
import { ApiErrorEquivalence } from '../lib/api-errors-equivalence';
import apiRequestList from '../../common/api-info/endpoints';
import {
ERROR_CODES,
HTTP_STATUS_CODES,
WAZUH_ERROR_DAEMONS_NOT_READY,
} from '../../common/constants';
Expand Down Expand Up @@ -199,14 +200,14 @@ export class WazuhApiCtrl {
);
}
} catch (error) {
if (error.code === 'EPROTO') {
if (error.code === ERROR_CODES.EPROTO) {
return response.ok({
body: {
statusCode: HTTP_STATUS_CODES.OK,
data: { apiIsDown: true },
},
});
} else if (error.code === 'ECONNREFUSED') {
} else if (error.code === ERROR_CODES.ECONNREFUSED) {
return response.ok({
body: {
statusCode: HTTP_STATUS_CODES.OK,
Expand Down Expand Up @@ -385,7 +386,7 @@ export class WazuhApiCtrl {
response,
);
}
if (error.code === 'EPROTO') {
if (error.code === ERROR_CODES.EPROTO) {
return ErrorResponse(
'Wrong protocol being used to connect to the API',
3005,
Expand Down Expand Up @@ -578,7 +579,7 @@ export class WazuhApiCtrl {
const check = await this.checkDaemons(context, api, path);
return check;
} catch (error) {
const isDown = (error || {}).code === 'ECONNREFUSED';
const isDown = error?.code === ERROR_CODES.ECONNREFUSED;
if (!isDown) {
context.wazuh.logger.error(
'Server API is online but the server is not ready yet',
Expand Down

0 comments on commit fcd0024

Please sign in to comment.