Created
June 29, 2021 14:50
-
-
Save paul-vd/5c542d3f809e76204031e141f9024fdb to your computer and use it in GitHub Desktop.
Example Error Instances
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
'use strict' | |
// Here is the base error classes to extend from | |
export class ApplicationError extends Error { | |
get name() { | |
return this.constructor.name | |
} | |
} | |
export class DatabaseError extends ApplicationError { | |
get code() { | |
return 500 | |
} | |
} | |
export class UserFacingError extends ApplicationError { | |
get code() { | |
return 400 | |
} | |
} | |
import { UserFacingError } from './base-errors' | |
/** UserFacingError - 400 */ | |
export class BadRequestError extends UserFacingError { | |
constructor(message, options = {}) { | |
super(message) | |
// You can attach relevant information to the error instance | |
// (e.g.. the username) | |
for (const [key, value] of Object.entries(options)) { | |
this[key] = value | |
} | |
} | |
get code() { | |
return 400 | |
} | |
} | |
/** UserFacingError - 401 */ | |
export class NotAuthorisedError extends UserFacingError { | |
constructor(message, options = {}) { | |
super(message) | |
// You can attach relevant information to the error instance | |
// (e.g.. the username) | |
for (const [key, value] of Object.entries(options)) { | |
this[key] = value | |
} | |
} | |
get code() { | |
return 401 | |
} | |
} | |
/** UserFacingError - 403 */ | |
export class ForbiddenError extends UserFacingError { | |
constructor(message, options = {}) { | |
super(message) | |
// You can attach relevant information to the error instance | |
// (e.g.. the username) | |
for (const [key, value] of Object.entries(options)) { | |
this[key] = value | |
} | |
} | |
get code() { | |
return 403 | |
} | |
} | |
/** UserFacingError - 404 */ | |
export class NotFoundError extends UserFacingError { | |
constructor(message, options = {}) { | |
super(message) | |
// You can attach relevant information to the error instance | |
// (e.g.. the username) | |
for (const [key, value] of Object.entries(options)) { | |
this[key] = value | |
} | |
} | |
get code() { | |
return 404 | |
} | |
} | |
/** UserFacingError - 409 */ | |
export class ConflictError extends UserFacingError { | |
constructor(message, options = {}) { | |
super(message) | |
// You can attach relevant information to the error instance | |
// (e.g.. the username) | |
for (const [key, value] of Object.entries(options)) { | |
this[key] = value | |
} | |
} | |
get code() { | |
return 409 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can then query errors, for example;