UNPKG

error-advisor

Version:

NPM module that aim to facilitate the error handling by providing http errors classes and also let you customize business errors as well with handy and clean way.

107 lines 5.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const StatusCode_1 = require("../enum/StatusCode"); const Client_Errors_1 = require("../error/client_error/Client_Errors"); const Server_Errors_1 = require("../error/server_error/Server_Errors"); class Utils { static errorBuilder(msg, type, statusCode, timestamp) { return { message: msg, error: type, status: statusCode, timestamp, stack: "", }; } static isClientError(statusCode) { return statusCode >= 400 && statusCode <= 499; } static isServerError(statusCode) { return statusCode >= 500 && statusCode <= 599; } /** * Utility function that create error object based on the status code given, * @param status * @param message Optional parameter if it's exist it's value will be used either the default value will be picked. */ static errorFactory(status, message) { switch (status) { case StatusCode_1.StatusCode.BAD_REQUEST: return new Client_Errors_1.BadRequest(message); case StatusCode_1.StatusCode.UNAUTHORIZED: return new Client_Errors_1.Unauthorized(message); case StatusCode_1.StatusCode.PAYMENT_REQUIRED: return new Client_Errors_1.PaymentRequied(message); case StatusCode_1.StatusCode.FORBIDDEN: return new Client_Errors_1.Forbidden(message); case StatusCode_1.StatusCode.NOT_FOUND: return new Client_Errors_1.NotFound(message); case StatusCode_1.StatusCode.METHOD_NOT_ALLOWED: return new Client_Errors_1.MethodNotAllowed(message); case StatusCode_1.StatusCode.NOT_ACCEPTABLE: return new Client_Errors_1.NotAcceptable(message); case StatusCode_1.StatusCode.PROXY_AUTHENTICATION_REQUIRED: return new Client_Errors_1.ProxyAuthenticationRequired(message); case StatusCode_1.StatusCode.REQUEST_TIMEOUT: return new Client_Errors_1.RequestTimeout(message); case StatusCode_1.StatusCode.CONFLICT: return new Client_Errors_1.Conflict(message); case StatusCode_1.StatusCode.GONE: return new Client_Errors_1.Gone(message); case StatusCode_1.StatusCode.LENGTH_REQUIRED: return new Client_Errors_1.LengthRequired(message); case StatusCode_1.StatusCode.PRECONDITION_FAILED: return new Client_Errors_1.PreconditionFailed(message); case StatusCode_1.StatusCode.PAYLOAD_TOO_LARGE: return new Client_Errors_1.PayloadTooLarge(message); case StatusCode_1.StatusCode.URI_TOO_LONG: return new Client_Errors_1.URITooLong(message); case StatusCode_1.StatusCode.UNSUPPORTED_MEDIA_TYPE: return new Client_Errors_1.UnsupportedMediaType(message); case StatusCode_1.StatusCode.RANGE_NOT_SATISFIABLE: return new Client_Errors_1.RangeNotSatisfiable(message); case StatusCode_1.StatusCode.EXPECTATION_FAILED: return new Client_Errors_1.ExcepectationFailed(message); case StatusCode_1.StatusCode.I_AM_A_TEAPOT: return new Client_Errors_1.IAmATeapot(message); case StatusCode_1.StatusCode.UNPROCESSABLE_ENTITY: return new Client_Errors_1.UnprocessableEntity(message); case StatusCode_1.StatusCode.TOO_EARLY: return new Client_Errors_1.TooEarly(message); case StatusCode_1.StatusCode.UPGRAFE_REQUIRED: return new Client_Errors_1.UpgradeRequired(message); case StatusCode_1.StatusCode.PRECONDITION_REQUIRED: return new Client_Errors_1.PreconditionRequired(message); case StatusCode_1.StatusCode.TOO_MANY_REQUESTS: return new Client_Errors_1.TooManyRequests(message); case StatusCode_1.StatusCode.REQUEST_HEADER_FIELDS_TOO_LARGE: return new Client_Errors_1.RequestHeaderFieldsTooLarge(message); case StatusCode_1.StatusCode.UNAVAILABLE_FOR_LEGAL_REASONS: return new Client_Errors_1.UnavailableForLegalReasons(message); case StatusCode_1.StatusCode.NOT_IMPLEMENTED: return new Server_Errors_1.NotImplemented(message); case StatusCode_1.StatusCode.BAD_GATEWAY: return new Server_Errors_1.BadGateway(message); case StatusCode_1.StatusCode.SERVICE_UNAVAILABLE: return new Server_Errors_1.ServiceUnavailable(message); case StatusCode_1.StatusCode.GATEWAY_TIMEOUT: return new Server_Errors_1.GatewayTimeout(message); case StatusCode_1.StatusCode.HTTP_VERSION_NOT_SUPPORTED: return new Server_Errors_1.HttpVersionNotSupported(message); case StatusCode_1.StatusCode.VARIANT_ALSO_NEGOTIATES: return new Server_Errors_1.VariantAlsoNegotiates(message); case StatusCode_1.StatusCode.INSUFFICIENT_STORAGE: return new Server_Errors_1.InsufficientStorage(message); case StatusCode_1.StatusCode.LOOP_DETECTED: return new Server_Errors_1.LoopDetected(message); case StatusCode_1.StatusCode.NOT_EXTENDED: return new Server_Errors_1.NotExtended(message); case StatusCode_1.StatusCode.NETWORK_AUTHENTICATION_REQUIRED: return new Server_Errors_1.NetworkAuthenticationRequired(message); default: return new Server_Errors_1.InternalServerError(message); } } } exports.default = Utils; //# sourceMappingURL=Utils.js.map