Skip to content

Commit

Permalink
feat(helpers): custom node error to deserve default api errors 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 20, 2019
1 parent 8f6f728 commit 0eab61d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/helpers/AppError.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/**
* Custom Errors with Node + Express
* Module dependencies
*/
const AppErrorCodes = {
serverError: 'SERVER_ERROR',
};

/**
* @desc Custom error class with Node + Express
* @param {String} message error
* @param {Object} { status, code }
*/

class AppError extends Error {
constructor(message, { status, code } = {}) {
super(message);

// Set HTTP status code
this.status = status || 500;

Expand All @@ -18,6 +23,9 @@ class AppError extends Error {
// Ensures that stack trace uses our subclass name
this.name = this.constructor.name;

// Share clean messages for api feedback
this.details = [message];

// Ensures the AppError subclass is sliced out of the
// stack trace dump for clarity
Error.captureStackTrace(this, this.constructor);
Expand Down

0 comments on commit 0eab61d

Please sign in to comment.