2022-05-02 12:52:33 +02:00
|
|
|
import {
|
|
|
|
BAD_REQUEST,
|
|
|
|
FORBIDDEN,
|
|
|
|
NOT_FOUND,
|
|
|
|
UNAUTHORIZED,
|
|
|
|
} from 'constants/statusCodes';
|
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
export interface IErrorBody {
|
|
|
|
details?: { message: string }[];
|
|
|
|
}
|
2022-02-11 11:19:55 +01:00
|
|
|
|
|
|
|
export class AuthenticationError extends Error {
|
2022-02-25 10:55:39 +01:00
|
|
|
statusCode: number;
|
|
|
|
|
2022-05-02 12:52:33 +02:00
|
|
|
constructor(statusCode: number = UNAUTHORIZED) {
|
2022-02-11 11:19:55 +01:00
|
|
|
super('Authentication required');
|
|
|
|
this.name = 'AuthenticationError';
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ForbiddenError extends Error {
|
2022-02-25 10:55:39 +01:00
|
|
|
statusCode: number;
|
|
|
|
body: IErrorBody;
|
|
|
|
|
2022-05-02 12:52:33 +02:00
|
|
|
constructor(statusCode: number = FORBIDDEN, body: IErrorBody = {}) {
|
2022-02-11 11:19:55 +01:00
|
|
|
super(
|
2022-02-25 10:55:39 +01:00
|
|
|
body.details?.length
|
2022-02-11 11:19:55 +01:00
|
|
|
? body.details[0].message
|
|
|
|
: 'You cannot perform this action'
|
|
|
|
);
|
|
|
|
this.name = 'ForbiddenError';
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
this.body = body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class BadRequestError extends Error {
|
2022-02-25 10:55:39 +01:00
|
|
|
statusCode: number;
|
|
|
|
body: IErrorBody;
|
|
|
|
|
2022-05-02 12:52:33 +02:00
|
|
|
constructor(statusCode: number = BAD_REQUEST, body: IErrorBody = {}) {
|
2022-02-25 10:55:39 +01:00
|
|
|
super(body.details?.length ? body.details[0].message : 'Bad request');
|
2022-02-11 11:19:55 +01:00
|
|
|
this.name = 'BadRequestError';
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
this.body = body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NotFoundError extends Error {
|
2022-02-25 10:55:39 +01:00
|
|
|
statusCode: number;
|
|
|
|
|
2022-05-02 12:52:33 +02:00
|
|
|
constructor(statusCode: number = NOT_FOUND) {
|
2022-06-09 15:36:01 +02:00
|
|
|
super('The requested resource could not be found.');
|
2022-02-11 11:19:55 +01:00
|
|
|
this.name = 'NotFoundError';
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 10:55:39 +01:00
|
|
|
|
2022-08-30 09:54:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 10:55:39 +01:00
|
|
|
export const headers = {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
};
|