1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00
unleash.unleash/frontend/src/utils/apiUtils.ts
Nuno Góis 6c21ed5f74
feat: make maintenance-related 503s more intuitive (#5018)
This makes maintenance-related 503s more intuitive on our UI by
mentioning that maintenance banner is currently enabled.


![image](https://github.com/Unleash/unleash/assets/14320932/43142c58-6b87-4b2d-9239-50f2bb1409e6)
2023-10-16 09:27:29 +01:00

89 lines
2.2 KiB
TypeScript

import {
BAD_REQUEST,
FORBIDDEN,
NOT_FOUND,
UNAUTHORIZED,
UNAVAILABLE,
} from 'constants/statusCodes';
export interface IErrorBody {
message?: string;
details?: { message: string }[];
}
const getErrorMessage = (body: IErrorBody) =>
body.details?.[0]?.message || body.message;
export class AuthenticationError extends Error {
statusCode: number;
constructor(statusCode: number = UNAUTHORIZED) {
super('Authentication required');
this.name = 'AuthenticationError';
this.statusCode = statusCode;
}
}
export class ForbiddenError extends Error {
statusCode: number;
body: IErrorBody;
constructor(statusCode: number = FORBIDDEN, body: IErrorBody = {}) {
super(getErrorMessage(body) || 'You cannot perform this action');
this.name = 'ForbiddenError';
this.statusCode = statusCode;
this.body = body;
}
}
export class UnavailableError extends Error {
statusCode: number;
body: IErrorBody;
constructor(statusCode: number = UNAVAILABLE, body: IErrorBody = {}) {
super(getErrorMessage(body) || 'This operation is unavailable');
this.name = 'UnavailableError';
this.statusCode = statusCode;
this.body = body;
}
}
export class BadRequestError extends Error {
statusCode: number;
body: IErrorBody;
constructor(statusCode: number = BAD_REQUEST, body: IErrorBody = {}) {
super(getErrorMessage(body) || 'Bad request');
this.name = 'BadRequestError';
this.statusCode = statusCode;
this.body = body;
}
}
export class NotFoundError extends Error {
statusCode: number;
constructor(statusCode: number = NOT_FOUND) {
super('The requested resource could not be found.');
this.name = 'NotFoundError';
this.statusCode = statusCode;
}
}
export class ResponseError extends Error {
status: number;
body: unknown;
constructor(target: string, status: number, body: unknown) {
super(`An error occurred while trying to get ${target}.`);
this.name = 'ResponseError';
this.status = status;
this.body = body;
}
}
export const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
};