Skip to content

Instantly share code, notes, and snippets.

@OliverBrotchie
Created July 18, 2022 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverBrotchie/82586b4ba11914cd07901732f6ccdb8e to your computer and use it in GitHub Desktop.
Save OliverBrotchie/82586b4ba11914cd07901732f6ccdb8e to your computer and use it in GitHub Desktop.
Custom errors
/**
* Similar to a standard Error, but with a custom message and status code field.
* @param status The status code of the error.
* @param message The message of the error.
*/
export class HttpError extends Error {
name = "HttpError";
status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
}
}
function constructErrorMessage(status:number,message:string):string {
// Do something with the input
return `${status} - ${message}`
}
/**
* Similar to a standard Error, but with a custom message and status code field.
* @param status The status code of the error.
* @param message The message of the error.
*/
export class HttpError extends Error {
name = "HttpError";
status: number;
constructor(status: number, message: string) {
super(constructErrorMessage());
this.status = status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment