74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const { Errors, HttpStatus } = 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, messageLong) {
|
|
super(message);
|
|
// Object.setPrototypeOf(this, new.target.prototype);
|
|
this.name = "AppError";
|
|
this.statusCode = HttpStatus.CONFLICT; // Default HTTP status code for generic Application errors
|
|
this.defaultMessage = Errors.UNKNOWN_APP_ERROR;
|
|
this.messageLong = messageLong;
|
|
// Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
// Error.captureStackTrace(this);
|
|
// Object.defineProperty(this, 'stack', { enumerable: true });
|
|
}
|
|
|
|
static create(message, messageLong) {
|
|
return new (clsMap[this.name])(message, messageLong);
|
|
}
|
|
|
|
static throw(message, messageLong) {
|
|
throw new (clsMap[this.name])(message, messageLong);
|
|
}
|
|
}
|
|
|
|
class AppAuthError extends AppError {
|
|
constructor(message = Errors.NO_ACCESS, messageLong) {
|
|
super(message, messageLong);
|
|
this.statusCode = HttpStatus.UNAUTHORIZED;
|
|
this.name = "AppAuthError";
|
|
this.defaultMessage = Errors.NO_ACCESS;
|
|
}
|
|
}
|
|
|
|
class AppParamError extends AppError {
|
|
constructor(message = Errors.INVALID_PARAM, messageLong) {
|
|
super(message, messageLong);
|
|
this.name = "AppParamError";
|
|
this.defaultMessage = Errors.INVALID_PARAM;
|
|
}
|
|
}
|
|
|
|
class AppInputError extends AppError {
|
|
constructor(message = Errors.INVALID_INPUT, messageLong) {
|
|
super(message, messageLong);
|
|
this.name = "AppInputError";
|
|
this.defaultMessage = Errors.INVALID_INPUT;
|
|
}
|
|
}
|
|
|
|
class AppMembershipError extends AppError {
|
|
constructor(message = Errors.SUBSCRIPTION_NOT_FOUND, messageLong) {
|
|
super(message, messageLong);
|
|
this.statusCode = HttpStatus.GONE;
|
|
this.name = "AppMembershipError";
|
|
this.defaultMessage = Errors.SUBSCRIPTION_NOT_FOUND;
|
|
}
|
|
}
|
|
|
|
|
|
const clsMap = {
|
|
'AppError': AppError,
|
|
'AppAuthError': AppAuthError,
|
|
'AppParamError': AppParamError,
|
|
'AppInputError': AppInputError,
|
|
'AppMembershipError': AppMembershipError
|
|
};
|
|
|
|
module.exports = {
|
|
AppError, AppAuthError, AppParamError, AppInputError, AppMembershipError
|
|
} |