1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00
mastodon/streaming/errors.js
Emelia Smith 491dd97642
Streaming: refactor to custom Error classes (#28632)
Co-authored-by: Renaud Chaput <renchap@gmail.com>
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
2024-02-22 13:20:20 +00:00

51 lines
1.2 KiB
JavaScript

// @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;