-
-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve API error-handling (#1301)
Unleash is an API and it would simplyfy a lot of the specific errors could carry the expected HTTP status code for this error. This would eliminate the need for a gigantic switch/case in the handle-errors function.
- Loading branch information
Showing
8 changed files
with
61 additions
and
12 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 |
---|---|---|
|
@@ -19,5 +19,5 @@ class BadDataError extends Error { | |
}; | ||
} | ||
} | ||
|
||
export default BadDataError; | ||
module.exports = BadDataError; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class BaseError extends Error { | ||
readonly statusCode: number; | ||
|
||
constructor(message: string, statusCode: number, name: string) { | ||
super(); | ||
|
||
this.name = name; | ||
this.message = message; | ||
this.statusCode = statusCode; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
|
||
toJSON(): object { | ||
return { | ||
isJoi: true, | ||
name: this.constructor.name, | ||
details: [ | ||
{ | ||
message: this.message, | ||
}, | ||
], | ||
}; | ||
} | ||
} | ||
|
||
export default BaseError; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import BaseError from './base-error'; | ||
|
||
class DisabledError extends BaseError { | ||
constructor(message: string) { | ||
super(message, 422, 'DisabledError'); | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
export default DisabledError; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import BaseError from './base-error'; | ||
|
||
class PasswordMismatch extends BaseError { | ||
constructor(message: string = 'Wrong password, try again.') { | ||
super(message, 401, 'PasswordMismatch'); | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
export default PasswordMismatch; |
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
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
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
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