-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from restyjs/errors
updated default error format
- Loading branch information
Showing
2 changed files
with
19 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,15 @@ | ||
export class HTTPError extends Error { | ||
statusCode: number; | ||
export class Exception extends Error { | ||
status: number = 500; | ||
code?: string; | ||
|
||
constructor(message: string, statusCode?: number) { | ||
super(); | ||
this.statusCode = statusCode ?? 500; | ||
this.message = message; | ||
constructor(message: string, status: number = 500, code?: string) { | ||
super(message); | ||
this.message = code ? `${code}: ${message}` : message; | ||
this.status = status; | ||
this.code = code; | ||
} | ||
} | ||
|
||
export class ValidationError extends HTTPError { | ||
errors: Object; | ||
export class HTTPError extends Exception { } | ||
|
||
constructor(errors: object) { | ||
var message = "Bad Request"; | ||
if (Array.isArray(errors)) { | ||
message = [ | ||
"failed to validate", | ||
errors.map((error) => error.property).join(", "), | ||
].join(" "); | ||
} | ||
super(message); | ||
this.statusCode = 400; | ||
this.errors = errors; | ||
} | ||
} | ||
export class ValidationError extends Exception { } |
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