UNPKG

@icapps/tree-house-errors

Version:

NodeJS default error definitions with an error parser utility function

79 lines (78 loc) 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticationError = exports.ValidationError = exports.UnauthorizedError = exports.InternalServerError = exports.ForbiddenError = exports.NotFoundError = exports.BadRequestError = exports.GenericError = exports.ApiError = void 0; const uuid = require("uuid"); const httpStatus = require("http-status"); const errors_config_1 = require("../config/errors.config"); class ApiError extends Error { constructor(status, error, args = {}) { const { message, detail, stack, status: customStatus } = args; super(message || error.message); this.name = 'ApiError'; this.isApiError = true; this.id = uuid.v1(); this.code = error.code; this.i18n = error.i18n; this.status = customStatus || status; this.detail = detail; if (stack) this.stack = stack; } } exports.ApiError = ApiError; class GenericError extends ApiError { constructor(error, args) { super(0, error == null ? errors_config_1.errors.GENERIC_ERROR : error, args); this.name = 'GenericError'; } } exports.GenericError = GenericError; class BadRequestError extends ApiError { constructor(error, args) { super(httpStatus.BAD_REQUEST, error == null ? errors_config_1.errors.BAD_REQUEST : error, args); this.name = 'BadRequestError'; } } exports.BadRequestError = BadRequestError; class NotFoundError extends ApiError { constructor(error, args) { super(httpStatus.NOT_FOUND, error == null ? errors_config_1.errors.RESOURCE_NOT_FOUND : error, args); this.name = 'NotFoundError'; } } exports.NotFoundError = NotFoundError; class ForbiddenError extends ApiError { constructor(error, args) { super(httpStatus.FORBIDDEN, error == null ? errors_config_1.errors.FORBIDDEN : error, args); this.name = 'ForbiddenError'; } } exports.ForbiddenError = ForbiddenError; class InternalServerError extends ApiError { constructor(error, args) { super(httpStatus.INTERNAL_SERVER_ERROR, error == null ? errors_config_1.errors.INTERNAL_ERROR : error, args); this.name = 'InternalServerError'; } } exports.InternalServerError = InternalServerError; class UnauthorizedError extends ApiError { constructor(error, args) { super(httpStatus.UNAUTHORIZED, error == null ? errors_config_1.errors.UNAUTHORIZED : error, args); this.name = 'UnauthorizedError'; } } exports.UnauthorizedError = UnauthorizedError; class ValidationError extends ApiError { constructor(error, args) { super(httpStatus.BAD_REQUEST, error == null ? errors_config_1.errors.INVALID_INPUT : error, args); this.name = 'ValidationError'; } } exports.ValidationError = ValidationError; class AuthenticationError extends ApiError { constructor(error, args) { super(httpStatus.BAD_REQUEST, error == null ? errors_config_1.errors.AUTHENTICATION_FAILED : error, args); this.name = 'AuthenticationError'; } } exports.AuthenticationError = AuthenticationError;