Skip to content

Instantly share code, notes, and snippets.

@sergio-august
Created November 27, 2020 16:35
Show Gist options
  • Save sergio-august/352f8ac5c51e752bcd4b3271f19fbf2d to your computer and use it in GitHub Desktop.
Save sergio-august/352f8ac5c51e752bcd4b3271f19fbf2d to your computer and use it in GitHub Desktop.
Better error handling in Express with HttpException classes
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