-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: clean up error handling (#33)
This change: - Normalizes response bodies to something like this: ``` { "error": { "code": "SOMETHING_BAD", "message": "Something went wrong" } } ``` - Adds error codes for various errors - Changes some 403s to 401s for correctness See also: <digidem/comapeo-core#955>
- Loading branch information
Showing
9 changed files
with
130 additions
and
33 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class HttpError extends Error { | ||
/** | ||
* @readonly | ||
* @prop {number} | ||
*/ | ||
statusCode | ||
|
||
/** | ||
* @readonly | ||
* @prop {string} | ||
*/ | ||
code | ||
|
||
/** | ||
* @param {number} statusCode | ||
* @param {Uppercase<string>} code | ||
* @param {string} message | ||
*/ | ||
constructor(statusCode, code, message) { | ||
super(message) | ||
this.statusCode = statusCode | ||
this.code = code | ||
} | ||
} | ||
|
||
export const invalidBearerToken = () => | ||
new HttpError(401, 'UNAUTHORIZED', 'Invalid bearer token') | ||
|
||
export const projectNotInAllowlist = () => | ||
new HttpError(403, 'PROJECT_NOT_IN_ALLOWLIST', 'Project not allowed') | ||
|
||
export const tooManyProjects = () => | ||
new HttpError( | ||
403, | ||
'TOO_MANY_PROJECTS', | ||
'Server is already linked to the maximum number of projects', | ||
) | ||
|
||
export const projectNotFoundError = () => | ||
new HttpError(404, 'PROJECT_NOT_FOUND', 'Project not found') | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
export const normalizeCode = (str) => { | ||
switch (str) { | ||
case 'FST_ERR_VALIDATION': | ||
return 'BAD_REQUEST' | ||
default: | ||
return str.toUpperCase().replace(/[^A-Z]/gu, '_') | ||
} | ||
} |
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
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
Oops, something went wrong.