Skip to content

Commit

Permalink
Create error-handler.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 4, 2024
1 parent 8873f72 commit 427312f
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { ErrorTypes } = require('./error-types');

class ErrorHandler {
static handle(error) {
if (error instanceof Error) {
console.error(`Error: ${error.message}`);
console.error(`Stack: ${error.stack}`);
} else {
console.error(`Unknown error: ${error}`);
}

switch (error.code) {
case ErrorTypes.INVALID_REQUEST:
return { statusCode: 400, message: 'Invalid request' };
case ErrorTypes.UNAUTHORIZED:
return { statusCode: 401, message: 'Unauthorized' };
case ErrorTypes.FORBIDDEN:
return { statusCode: 403, message: 'Forbidden' };
case ErrorTypes.NOT_FOUND:
return { statusCode: 404, message: 'Not found' };
case ErrorTypes.INTERNAL_SERVER_ERROR:
return { statusCode: 500, message: 'Internal server error' };
default:
return { statusCode: 500, message: 'Unknown error' };
}
}
}

module.exports = ErrorHandler;

0 comments on commit 427312f

Please sign in to comment.