Created
November 27, 2020 16:35
-
-
Save sergio-august/352f8ac5c51e752bcd4b3271f19fbf2d to your computer and use it in GitHub Desktop.
Better error handling in Express with HttpException classes
This file contains hidden or 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
export abstract class HttpException extends Error { | |
status: number; | |
constructor(status: number, message: string) { | |
super(message); | |
this.status = status; | |
this.name = "HttpError"; | |
} | |
} | |
export class HttpBadRequestException extends HttpException { | |
constructor(message = "Incorrect request") { | |
super(400, message); | |
this.name = "HttpBadRequestError"; | |
} | |
} | |
export class HttpUnauthorizedException extends HttpException { | |
constructor(message = "Not authorized") { | |
super(401, message); | |
this.name = "HttpUnauthorizedError"; | |
} | |
} | |
export class HttpForbiddenException extends HttpException { | |
constructor(message = "Forbidden") { | |
super(403, message); | |
this.name = "HttpForbiddenError"; | |
} | |
} | |
export class HttpNotFoundException extends HttpException { | |
constructor(message = "Not found") { | |
super(404, message); | |
this.name = "HttpNotFoundError"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment