Skip to content

Commit

Permalink
Added loglevel to errors beign handled and added FileNotFound case
Browse files Browse the repository at this point in the history
  • Loading branch information
konzz authored and daneryl committed Sep 19, 2024
1 parent 915289f commit 0e95121
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/api/preserve/specs/preserve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Preserve', () => {
];
await testingEnvironment.setUp({ ...fixtures, settings: newSettings }, 'preserve-index');

await expect(Preserve.setup('en', { _id: 'someid' })).rejects.toEqual({
await expect(Preserve.setup('en', { _id: 'someid' })).rejects.toMatchObject({
message: 'Preserve configuration not found',
code: 402,
});
Expand Down
2 changes: 1 addition & 1 deletion app/api/templates/specs/templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('templates', () => {
await templates.save(changedTemplate);
throw new Error('properties have swaped names, should have failed with an error');
} catch (error) {
expect(error).toEqual({ code: 400, message: "Properties can't swap names: text" });
expect(error).toMatchObject({ code: 400, message: "Properties can't swap names: text" });
}
});

Expand Down
4 changes: 2 additions & 2 deletions app/api/users/specs/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('Users', () => {
_id: userId.toString(),
username: 'user name',
};
await expect(users.save(userdata, currentUser)).rejects.toEqual({
await expect(users.save(userdata, currentUser)).rejects.toMatchObject({
code: 400,
message: 'Usernames can not contain spaces.',
});
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('Users', () => {
role: 'editor',
groups: [],
};
await expect(users.newUser(userdata, domain)).rejects.toEqual({
await expect(users.newUser(userdata, domain)).rejects.toMatchObject({
code: 400,
message: 'Usernames can not contain spaces.',
});
Expand Down
15 changes: 11 additions & 4 deletions app/api/utils/Error.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
export default function (message, code = 500) {
/**
* @deprecated
*/
export default function (message, code = 500, logLevel = 'debug') {
if (code === 500) {
logLevel = 'error';
}

if (message.ajv) {
return { message: message.message, ...message, code };
return { message: message.message, ...message, code, logLevel };
}

if (message instanceof Error) {
return { message: message.message, stack: message.stack, code, original: message };
return { message: message.message, stack: message.stack, code, original: message, logLevel };
}

return { message, code };
return { message, code, logLevel };
}
32 changes: 24 additions & 8 deletions app/api/utils/handleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createError } from 'api/utils/index';
import { appContext } from 'api/utils/AppContext';
import { UnauthorizedError } from 'api/authorization.v2/errors/UnauthorizedError';
import { ValidationError } from 'api/common.v2/validation/ValidationError';
import { FileNotFound } from 'api/files/FileNotFound';

const ajvPrettifier = error => {
const errorMessage = [error.message];
Expand Down Expand Up @@ -58,39 +59,53 @@ const prettifyError = (error, { req = {}, uncaught = false } = {}) => {
let result = error;

if (error instanceof Error) {
result = { code: 500, message: error.stack };
result = { code: 500, message: error.stack, logLevel: 'error' };
}

if (error instanceof Ajv.ValidationError) {
result = { code: 422, message: error.message, validations: error.errors };
result = { code: 422, message: error.message, validations: error.errors, logLevel: 'debug' };
}

if (error.name === 'ValidationError') {
result = { code: 422, message: error.message, validations: error.properties };
result = {
code: 422,
message: error.message,
validations: error.properties,
logLevel: 'debug',
};
}

if (error instanceof ValidationError) {
result = { code: 422, message: error.message, validations: error.errors };
result = { code: 422, message: error.message, validations: error.errors, logLevel: 'debug' };
}

if (error instanceof UnauthorizedError) {
result = { code: 401, message: error.message };
result = { code: 401, message: error.message, logLevel: 'debug' };
}

if (error instanceof FileNotFound) {
result = { code: 404, message: error.message, logLevel: 'debug' };
}

if (error.name === 'MongoError') {
result.code = 500;
result.logLevel = 'error';
}

if (error.message && error.message.match(/Cast to ObjectId failed for value/)) {
result.code = 400;
result.logLevel = 'debug';
}

if (error.message && error.message.match(/rison decoder error/)) {
result.code = 400;
result.logLevel = 'debug';
}

if (uncaught) {
result.message = `uncaught exception or unhandled rejection, Node process finished !!\n ${result.message}`;
result.logLevel = 'error';
result.code = 500;
}

const obfuscatedRequest = obfuscateCredentials(req);
Expand Down Expand Up @@ -119,11 +134,12 @@ const getErrorMessage = (data, error) => {

const sendLog = (data, error, errorOptions) => {
const messageToLog = getErrorMessage(data, error);
if (data.code === 500) {
legacyLogger.error(messageToLog, errorOptions);
} else if (data.code === 400) {
if (data.logLevel === 'debug') {
legacyLogger.debug(messageToLog, errorOptions);
return;
}

legacyLogger.error(messageToLog, errorOptions);
};

function setRequestId(result) {
Expand Down
1 change: 1 addition & 0 deletions app/api/utils/specs/__snapshots__/handleError.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`handleError errors by type when error is created with createError should return the error 1`] = `
Object {
"code": 400,
"logLevel": "debug",
"message": "test error",
"prettyMessage": "
test error",
Expand Down

0 comments on commit 0e95121

Please sign in to comment.