forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Streaming: refactor to custom Error classes (mastodon#28632)
Co-authored-by: Renaud Chaput <[email protected]> Co-authored-by: Claire <[email protected]>
- Loading branch information
1 parent
ebe2086
commit 491dd97
Showing
2 changed files
with
103 additions
and
42 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,51 @@ | ||
// @ts-check | ||
|
||
/** | ||
* Typed as a string because otherwise it's a const string, which means we can't | ||
* override it in let statements. | ||
* @type {string} | ||
*/ | ||
const UNEXPECTED_ERROR_MESSAGE = 'An unexpected error occurred'; | ||
exports.UNKNOWN_ERROR_MESSAGE = UNEXPECTED_ERROR_MESSAGE; | ||
|
||
/** | ||
* Extracts the status and message properties from the error object, if | ||
* available for public use. The `unknown` is for catch statements | ||
* @param {Error | AuthenticationError | RequestError | unknown} err | ||
*/ | ||
exports.extractStatusAndMessage = function(err) { | ||
let statusCode = 500; | ||
let errorMessage = UNEXPECTED_ERROR_MESSAGE; | ||
if (err instanceof AuthenticationError || err instanceof RequestError) { | ||
statusCode = err.status; | ||
errorMessage = err.message; | ||
} | ||
|
||
return { statusCode, errorMessage }; | ||
}; | ||
|
||
class RequestError extends Error { | ||
/** | ||
* @param {string} message | ||
*/ | ||
constructor(message) { | ||
super(message); | ||
this.name = "RequestError"; | ||
this.status = 400; | ||
} | ||
} | ||
|
||
exports.RequestError = RequestError; | ||
|
||
class AuthenticationError extends Error { | ||
/** | ||
* @param {string} message | ||
*/ | ||
constructor(message) { | ||
super(message); | ||
this.name = "AuthenticationError"; | ||
this.status = 401; | ||
} | ||
} | ||
|
||
exports.AuthenticationError = AuthenticationError; |
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