1
0
Fork 0
nodejs-backend-template/server/utils/application-error.js

33 lines
1.5 KiB
JavaScript

/**
* The `ApplicationError` class represents a special error that can occur in the program.
* This class allows you to create errors with different codes, HTTP statuses and messages.
*
* @class
* @extends Error
*/
class ApplicationError extends Error {
static NotUnique = (message) => new ApplicationError(1, 409, message || 'The item is not unique');
static NotFound = () => new ApplicationError(2, 404, 'Item not found');
static NotCreated = () => new ApplicationError(3, 400, 'Element not created');
static RequiredAttributes = () => new ApplicationError(4, 400, 'Required attributes are not specified');
static JsonValidation = (message) => new ApplicationError(5, 422, `Invalid JSON format: ${message}`);
static AlreadyExists = () => new ApplicationError(6, 409, 'Already exists');
static BadTransaction = () => new ApplicationError(7, 400, 'Invalid transaction');
static InvalidCredentials = () => new ApplicationError(8, 401, 'Invalid credentials');
static BadToken = () => new ApplicationError(9, 401, 'Bad token');
static AdminTokenNotFound = () => new ApplicationError(10, 401, 'Admin token not found');
static NoAuthHeader = () => new ApplicationError(11, 403, 'Authorization header not specified');
constructor(code, httpStatusCode, message, body) {
super();
this.code = code;
this.httpStatusCode = httpStatusCode;
this.message = message;
this.body = body;
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = ApplicationError;