95 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-08-09 19:14:27 +02:00
/* eslint-disable max-classes-per-file */
2021-10-20 17:30:05 +02:00
'use strict';
const { HttpError } = require('http-errors');
const { formatYupErrors } = require('./format-yup-error');
/* ApplicationError */
class ApplicationError extends Error {
constructor(message, details = {}) {
super();
this.name = 'ApplicationError';
this.message = message || 'An application error occured';
this.details = details;
}
}
class ValidationError extends ApplicationError {
constructor(message, details) {
super(message, details);
this.name = 'ValidationError';
}
}
class YupValidationError extends ValidationError {
2021-10-20 17:30:05 +02:00
constructor(yupError, message) {
super();
const { errors, message: yupMessage } = formatYupErrors(yupError);
this.message = message || yupMessage;
this.details = { errors };
}
}
class PaginationError extends ApplicationError {
constructor(message, details) {
2021-10-20 17:30:05 +02:00
super(message, details);
this.name = 'PaginationError';
this.message = message || 'Invalid pagination';
}
}
2021-10-27 18:54:58 +02:00
class NotFoundError extends ApplicationError {
2021-10-20 17:30:05 +02:00
constructor(message, details) {
super(message, details);
2021-10-27 18:54:58 +02:00
this.name = 'NotFoundError';
this.message = message || 'Entity not found';
}
}
class ForbiddenError extends ApplicationError {
constructor(message, details) {
super(message, details);
this.name = 'ForbiddenError';
2021-10-28 12:10:22 +02:00
this.message = message || 'Forbidden access';
2021-10-27 18:54:58 +02:00
}
}
class PayloadTooLargeError extends ApplicationError {
constructor(message, details) {
super(message, details);
this.name = 'PayloadTooLargeError';
this.message = message || 'Entity too large';
}
}
class UnauthorizedError extends ApplicationError {
constructor(message, details) {
super(message, details);
this.name = 'UnauthorizedError';
this.message = message || 'Unauthorized';
2021-10-20 17:30:05 +02:00
}
}
class PolicyError extends ForbiddenError {
constructor(message, details) {
super(message, details);
this.name = 'PolicyError';
this.message = message || 'Policy Failed';
this.details = details || {};
}
}
2021-10-20 17:30:05 +02:00
module.exports = {
HttpError,
ApplicationError,
ValidationError,
YupValidationError,
PaginationError,
2021-10-27 18:54:58 +02:00
NotFoundError,
ForbiddenError,
PayloadTooLargeError,
UnauthorizedError,
PolicyError,
2021-10-20 17:30:05 +02:00
};