@alba-cars/common-modules
Version:
A package containing DTOs, validation classes and common modules and interfaces for Alba Cars
49 lines (48 loc) • 1.94 kB
JavaScript
;
// AppError.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppError = exports.ErrorType = void 0;
var ErrorType;
(function (ErrorType) {
ErrorType["VALIDATION_ERROR"] = "VALIDATION_ERROR";
ErrorType["NOT_FOUND"] = "NOT_FOUND";
ErrorType["UNAUTHORIZED"] = "UNAUTHORIZED";
ErrorType["FORBIDDEN"] = "FORBIDDEN";
ErrorType["INTERNAL_SERVER_ERROR"] = "INTERNAL_SERVER_ERROR";
ErrorType["BAD_REQUEST"] = "BAD_REQUEST";
ErrorType["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
ErrorType["AUTHORIZATION_ERROR"] = "AUTHORIZATION_ERROR";
})(ErrorType = exports.ErrorType || (exports.ErrorType = {}));
class AppError extends Error {
constructor(message, statusCode, type, details) {
super(message);
this.message = message;
this.statusCode = statusCode;
this.type = type;
this.details = details;
this.statusCode = statusCode;
this.type = type;
this.details = details;
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
static badRequest(message, details) {
return new AppError(message, 400, ErrorType.BAD_REQUEST, details);
}
static notFound(message, details) {
return new AppError(message, 404, ErrorType.NOT_FOUND, details);
}
static unauthorized(message, details) {
return new AppError(message, 401, ErrorType.UNAUTHORIZED, details);
}
static forbidden(message, details) {
return new AppError(message, 403, ErrorType.FORBIDDEN, details);
}
static internal(message, details) {
return new AppError(message, 500, ErrorType.INTERNAL_SERVER_ERROR, details);
}
static validationError(message, details) {
return new AppError(message, 422, ErrorType.VALIDATION_ERROR, details);
}
}
exports.AppError = AppError;