Skip to content

Instantly share code, notes, and snippets.

@miguelrk
Created March 18, 2024 18:27
Show Gist options
  • Save miguelrk/1b10029bce5b7b77074273e96ae299cb to your computer and use it in GitHub Desktop.
Save miguelrk/1b10029bce5b7b77074273e96ae299cb to your computer and use it in GitHub Desktop.
A custom Error class for HTTP Response errors with a toResponse() method for conversion to HTTP Responses.
const errors: Record<string, { status: number; message: string }> = {
BadRequest: { status: 400, message: "Bad Request" },
Unauthorized: { status: 401, message: "Unauthorized" },
PaymentRequired: { status: 402, message: "Payment Required" },
Forbidden: { status: 403, message: "Forbidden" },
NotFound: { status: 404, message: "Not Found" },
MethodNotAllowed: { status: 405, message: "Method Not Allowed" },
InternalServerError: { status: 500, message: "Internal Server Error" },
NotImplemented: { status: 501, message: "Not Implemented" },
BadGateway: { status: 502, message: "Bad Gateway" },
ServiceUnavailable: { status: 503, message: "Service Unavailable" },
};
export class ResponseError extends Error {
errorName: keyof typeof errors;
status: number;
message: string;
constructor(errorName: keyof typeof errors, message?: string) {
const errorDefaults = errors[errorName];
if (!errorDefaults) {
throw new Error(`Unknown error name: ${errorName}`);
}
super(message || errorDefaults.message);
this.name = "ResponseError";
this.errorName = errorName;
this.status = errorDefaults.status;
this.message = message || errorDefaults.message;
Object.setPrototypeOf(this, ResponseError.prototype); // set prototype explicitly
}
toResponse(): Response {
return new Response(`${this.errorName}: ${this.message}`, {
status: this.status,
statusText: this.message,
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment