Created
July 18, 2022 12:21
-
-
Save OliverBrotchie/82586b4ba11914cd07901732f6ccdb8e to your computer and use it in GitHub Desktop.
Custom errors
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
/** | |
* 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; | |
} | |
} |
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
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