-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(error): generic error handler for API requests
- Loading branch information
Showing
3 changed files
with
40 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
const ApiErrorCodes = { | ||
serverError: 'SERVER_ERROR' | ||
} | ||
|
||
class ApiError extends Error { | ||
constructor (message, {status, code} = {}) { | ||
super(message) | ||
|
||
// Set Generic error message | ||
this.message = message | ||
|
||
// Set HTTP status code | ||
this.status = status || 500 | ||
|
||
// Set API error code | ||
this.code = code || ApiErrorCodes.serverError | ||
|
||
// Ensures that stack trace uses our subclass name | ||
this.name = this.constructor.name | ||
|
||
// Ensures the ApiError subclass is sliced out of the | ||
// stack trace dump for clarity | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} | ||
|
||
module.exports = ApiError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters