agmission/Development/server/helpers/app_error.js

65 lines
1.6 KiB
JavaScript

'use strict';
const { Errors } = require('./constants');
/* Enhanced Error Handling with custom error classes for better maintenance, easier debugging and avoiding potential bugs */
class AppError extends Error {
constructor(message = Errors.UNKNOWN_APP_ERROR, statusCode = 409) {
super(message);
// Object.setPrototypeOf(this, new.target.prototype);
this.name = "AppError";
this.statusCode = statusCode;
// Error.captureStackTrace(this);
// Object.defineProperty(this, 'stack', { enumerable: true });
}
static create(message, statusCode) {
return new (clsMap[this.name])(message, statusCode);
}
static throw(message, statusCode) {
throw new (clsMap[this.name])(message, statusCode);
}
}
class AppAuthError extends AppError {
constructor(message = Errors.NO_ACCESS, statusCode = 401) {
super(message, statusCode);
this.name = "AppAuthError";
}
}
class AppParamError extends AppError {
constructor(message = Errors.INVALID_PARAM) {
super(message);
this.name = "AppParamError";
}
}
class AppInputError extends AppError {
constructor(message = Errors.INVALID_INPUT) {
super(message);
this.name = "AppInputError";
}
}
class AppMembershipError extends AppError {
constructor(message = Errors.SUBSCRIPTION_NOT_FOUND, statusCode = 410) {
super(message, statusCode);
this.name = "AppMembershipError";
}
}
const clsMap = {
'AppError': AppError,
'AppAuthError': AppAuthError,
'AppParamError': AppParamError,
'AppInputError': AppInputError,
'AppMembershipError': AppMembershipError
};
module.exports = {
AppError, AppAuthError, AppParamError, AppInputError, AppMembershipError
}