Skip to content

Instantly share code, notes, and snippets.

@ThomasAribart
Last active May 15, 2020 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasAribart/f1a24d96924a4826a63b3aabf7ee6fd3 to your computer and use it in GitHub Desktop.
Save ThomasAribart/f1a24d96924a4826a63b3aabf7ee6fd3 to your computer and use it in GitHub Desktop.
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