2024-02-22 14:20:20 +01:00
|
|
|
// @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}
|
|
|
|
*/
|
2024-02-27 15:59:20 +01:00
|
|
|
export const UNEXPECTED_ERROR_MESSAGE = 'An unexpected error occurred';
|
2024-02-22 14:20:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2024-02-27 15:59:20 +01:00
|
|
|
export function extractStatusAndMessage(err) {
|
2024-02-22 14:20:20 +01:00
|
|
|
let statusCode = 500;
|
|
|
|
let errorMessage = UNEXPECTED_ERROR_MESSAGE;
|
|
|
|
if (err instanceof AuthenticationError || err instanceof RequestError) {
|
|
|
|
statusCode = err.status;
|
|
|
|
errorMessage = err.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { statusCode, errorMessage };
|
2024-02-27 15:59:20 +01:00
|
|
|
}
|
2024-02-22 14:20:20 +01:00
|
|
|
|
2024-02-27 15:59:20 +01:00
|
|
|
export class RequestError extends Error {
|
2024-02-22 14:20:20 +01:00
|
|
|
/**
|
|
|
|
* @param {string} message
|
|
|
|
*/
|
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
|
|
|
this.name = "RequestError";
|
|
|
|
this.status = 400;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 15:59:20 +01:00
|
|
|
export class AuthenticationError extends Error {
|
2024-02-22 14:20:20 +01:00
|
|
|
/**
|
|
|
|
* @param {string} message
|
|
|
|
*/
|
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
|
|
|
this.name = "AuthenticationError";
|
|
|
|
this.status = 401;
|
|
|
|
}
|
|
|
|
}
|