Last active
May 15, 2020 12:19
-
-
Save ThomasAribart/f1a24d96924a4826a63b3aabf7ee6fd3 to your computer and use it in GitHub Desktop.
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
class CustomError extends Error { | |
constructor(statusCode, { key, message }) { | |
super(message); | |
this.statusCode = statusCode; | |
this.key = key; | |
} | |
} | |
export class BadRequestError extends CustomError { | |
constructor({ | |
key = 'BAD_REQUEST', | |
message = 'Bad Request', | |
} = {}) { | |
super(400, { key, message }); | |
} | |
} | |
class InternalServerError extends CustomError { | |
constructor({ | |
key = 'INTERNAL_SERVER_ERROR', | |
message = 'Internal Server Error', | |
} = {}) { | |
super(500, { key, message }); | |
} | |
} | |
const error = new Error(); | |
error instanceof CustomError; // false | |
const internalServerError = new InternalServerError(); | |
internalServerError instanceof CustomError; // true | |
internalServerError.statusCode; // 500 | |
internalServerError.key; // INTERNAL_SERVER_ERROR | |
internalServerError.message; // Internal Server Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment