UNPKG

error-express

Version:

A lightweight, zero-dependency package to handle global errors in Express.js applications. It catches unhandled errors and provides a simple, yet powerful way to manage them.

2 lines 8.47 kB
"use strict";var V=Object.create;var n=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,q=Object.prototype.hasOwnProperty;var K=(E,e)=>{for(var R in e)n(E,R,{get:e[R],enumerable:!0})},G=(E,e,R,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let O of b(e))!q.call(E,O)&&O!==R&&n(E,O,{get:()=>e[O],enumerable:!(o=f(e,O))||o.enumerable});return E};var H=(E,e,R)=>(R=E!=null?V(W(E)):{},G(e||!E||!E.__esModule?n(R,"default",{value:E,enumerable:!0}):R,E)),X=E=>G(n({},"__esModule",{value:!0}),E);var z={};K(z,{AppError:()=>T,BadGatewayError:()=>M,BadRequestError:()=>N,ConflictError:()=>u,DefaultStatusMessages:()=>s,ForbiddenError:()=>c,GatewayTimeoutError:()=>F,GoneError:()=>P,HttpStatusCodes:()=>t,InsufficientStorageError:()=>g,MethodNotAllowedError:()=>D,NotAcceptableError:()=>i,NotFoundError:()=>A,NotImplementedError:()=>l,PayloadTooLargeError:()=>L,PaymentRequiredError:()=>C,PreconditionFailedError:()=>d,RequestTimeoutError:()=>S,ServerError:()=>_,ServiceUnavailableError:()=>x,TooManyRequestsError:()=>U,UnauthorizedError:()=>I,UnprocessableEntityError:()=>a,UnsupportedMediaTypeError:()=>p,globalErrorHandler:()=>j,handleError:()=>y});module.exports=X(z);var Q=H(require("express/lib/router/layer")),Y=H(require("express/lib/router"));var t={CONTINUE:100,SWITCHING_PROTOCOLS:101,PROCESSING:102,EARLY_HINTS:103,OK:200,CREATED:201,ACCEPTED:202,NON_AUTHORITATIVE_INFORMATION:203,NO_CONTENT:204,RESET_CONTENT:205,PARTIAL_CONTENT:206,MULTI_STATUS:207,ALREADY_REPORTED:208,IM_USED:226,MULTIPLE_CHOICES:300,MOVED_PERMANENTLY:301,FOUND:302,SEE_OTHER:303,NOT_MODIFIED:304,USE_PROXY:305,TEMPORARY_REDIRECT:307,PERMANENT_REDIRECT:308,BAD_REQUEST:400,UNAUTHORIZED:401,PAYMENT_REQUIRED:402,FORBIDDEN:403,NOT_FOUND:404,METHOD_NOT_ALLOWED:405,NOT_ACCEPTABLE:406,PROXY_AUTHENTICATION_REQUIRED:407,REQUEST_TIMEOUT:408,CONFLICT:409,GONE:410,LENGTH_REQUIRED:411,PRECONDITION_FAILED:412,PAYLOAD_TOO_LARGE:413,URI_TOO_LONG:414,UNSUPPORTED_MEDIA_TYPE:415,RANGE_NOT_SATISFIABLE:416,EXPECTATION_FAILED:417,IM_A_TEAPOT:418,MISDIRECTED_REQUEST:421,UNPROCESSABLE_ENTITY:422,LOCKED:423,FAILED_DEPENDENCY:424,TOO_EARLY:425,UPGRADE_REQUIRED:426,PRECONDITION_REQUIRED:428,TOO_MANY_REQUESTS:429,REQUEST_HEADER_FIELDS_TOO_LARGE:431,UNAVAILABLE_FOR_LEGAL_REASONS:451,INTERNAL_SERVER_ERROR:500,NOT_IMPLEMENTED:501,BAD_GATEWAY:502,SERVICE_UNAVAILABLE:503,GATEWAY_TIMEOUT:504,HTTP_VERSION_NOT_SUPPORTED:505,VARIANT_ALSO_NEGOTIATES:506,INSUFFICIENT_STORAGE:507,LOOP_DETECTED:508,NOT_EXTENDED:510,NETWORK_AUTHENTICATION_REQUIRED:511},s={[t.CONTINUE]:"Continue",[t.SWITCHING_PROTOCOLS]:"Switching Protocols",[t.PROCESSING]:"Processing",[t.EARLY_HINTS]:"Early Hints",[t.OK]:"OK",[t.CREATED]:"Created",[t.ACCEPTED]:"Accepted",[t.NON_AUTHORITATIVE_INFORMATION]:"Non-Authoritative Information",[t.NO_CONTENT]:"No Content",[t.RESET_CONTENT]:"Reset Content",[t.PARTIAL_CONTENT]:"Partial Content",[t.MULTI_STATUS]:"Multi-Status",[t.ALREADY_REPORTED]:"Already Reported",[t.IM_USED]:"IM Used",[t.MULTIPLE_CHOICES]:"Multiple Choices",[t.MOVED_PERMANENTLY]:"Moved Permanently",[t.FOUND]:"Found",[t.SEE_OTHER]:"See Other",[t.NOT_MODIFIED]:"Not Modified",[t.USE_PROXY]:"Use Proxy",[t.TEMPORARY_REDIRECT]:"Temporary Redirect",[t.PERMANENT_REDIRECT]:"Permanent Redirect",[t.BAD_REQUEST]:"Bad Request",[t.UNAUTHORIZED]:"Unauthorized",[t.PAYMENT_REQUIRED]:"Payment Required",[t.FORBIDDEN]:"Forbidden",[t.NOT_FOUND]:"Not Found",[t.METHOD_NOT_ALLOWED]:"Method Not Allowed",[t.NOT_ACCEPTABLE]:"Not Acceptable",[t.PROXY_AUTHENTICATION_REQUIRED]:"Proxy Authentication Required",[t.REQUEST_TIMEOUT]:"Request Timeout",[t.CONFLICT]:"Conflict",[t.GONE]:"Gone",[t.LENGTH_REQUIRED]:"Length Required",[t.PRECONDITION_FAILED]:"Precondition Failed",[t.PAYLOAD_TOO_LARGE]:"Payload Too Large",[t.URI_TOO_LONG]:"URI Too Long",[t.UNSUPPORTED_MEDIA_TYPE]:"Unsupported Media Type",[t.RANGE_NOT_SATISFIABLE]:"Range Not Satisfiable",[t.EXPECTATION_FAILED]:"Expectation Failed",[t.IM_A_TEAPOT]:"I'm a teapot",[t.MISDIRECTED_REQUEST]:"Misdirected Request",[t.UNPROCESSABLE_ENTITY]:"Unprocessable Entity",[t.LOCKED]:"Locked",[t.FAILED_DEPENDENCY]:"Failed Dependency",[t.TOO_EARLY]:"Too Early",[t.UPGRADE_REQUIRED]:"Upgrade Required",[t.PRECONDITION_REQUIRED]:"Precondition Required",[t.TOO_MANY_REQUESTS]:"Too Many Requests",[t.REQUEST_HEADER_FIELDS_TOO_LARGE]:"Request Header Fields Too Large",[t.UNAVAILABLE_FOR_LEGAL_REASONS]:"Unavailable For Legal Reasons",[t.INTERNAL_SERVER_ERROR]:"Internal Server Error",[t.NOT_IMPLEMENTED]:"Not Implemented",[t.BAD_GATEWAY]:"Bad Gateway",[t.SERVICE_UNAVAILABLE]:"Service Unavailable",[t.GATEWAY_TIMEOUT]:"Gateway Timeout",[t.HTTP_VERSION_NOT_SUPPORTED]:"HTTP Version Not Supported",[t.VARIANT_ALSO_NEGOTIATES]:"Variant Also Negotiates",[t.INSUFFICIENT_STORAGE]:"Insufficient Storage",[t.LOOP_DETECTED]:"Loop Detected",[t.NOT_EXTENDED]:"Not Extended",[t.NETWORK_AUTHENTICATION_REQUIRED]:"Network Authentication Required"};var m=E=>Object.keys(t).find(R=>t[R]===E)??"INTERNAL_SERVER_ERROR",T=class E extends Error{constructor(R,o,O=!0,r){super(R);this.message=R;this.statusCode=o;this.isOparational=O;this.code=r??m(o),Object.setPrototypeOf(this,E.prototype)}code},_=class extends T{constructor(e,R=t.INTERNAL_SERVER_ERROR){super(e,R,!0)}},N=class extends T{constructor(e=s[t.BAD_REQUEST]){super(e,t.BAD_REQUEST,!0)}},A=class extends T{constructor(e=s[t.NOT_FOUND]){super(e,t.NOT_FOUND,!0)}},I=class extends T{constructor(e=s[t.UNAUTHORIZED]){super(e,t.UNAUTHORIZED,!0)}},c=class extends T{constructor(e=s[t.FORBIDDEN]){super(e,t.FORBIDDEN,!0)}},u=class extends T{constructor(e=s[t.CONFLICT]){super(e,t.CONFLICT,!0)}},a=class extends T{constructor(e=s[t.UNPROCESSABLE_ENTITY]){super(e,t.UNPROCESSABLE_ENTITY,!0)}},D=class extends T{constructor(e=s[t.METHOD_NOT_ALLOWED]){super(e,t.METHOD_NOT_ALLOWED,!0)}},i=class extends T{constructor(e=s[t.NOT_ACCEPTABLE]){super(e,t.NOT_ACCEPTABLE,!0)}},S=class extends T{constructor(e=s[t.REQUEST_TIMEOUT]){super(e,t.REQUEST_TIMEOUT,!0)}},L=class extends T{constructor(e=s[t.PAYLOAD_TOO_LARGE]){super(e,t.PAYLOAD_TOO_LARGE,!0)}},p=class extends T{constructor(e=s[t.UNSUPPORTED_MEDIA_TYPE]){super(e,t.UNSUPPORTED_MEDIA_TYPE,!0)}},U=class extends T{constructor(e=s[t.TOO_MANY_REQUESTS]){super(e,t.TOO_MANY_REQUESTS,!0)}},C=class extends T{constructor(e=s[t.PAYMENT_REQUIRED]){super(e,t.PAYMENT_REQUIRED,!0)}},P=class extends T{constructor(e=s[t.GONE]){super(e,t.GONE,!0)}},d=class extends T{constructor(e=s[t.PRECONDITION_FAILED]){super(e,t.PRECONDITION_FAILED,!0)}},l=class extends T{constructor(e=s[t.NOT_IMPLEMENTED]){super(e,t.NOT_IMPLEMENTED,!0)}},M=class extends T{constructor(e=s[t.BAD_GATEWAY]){super(e,t.BAD_GATEWAY,!0)}},x=class extends T{constructor(e=s[t.SERVICE_UNAVAILABLE]){super(e,t.SERVICE_UNAVAILABLE,!0)}},F=class extends T{constructor(e=s[t.GATEWAY_TIMEOUT]){super(e,t.GATEWAY_TIMEOUT,!0)}},g=class extends T{constructor(e=s[t.INSUFFICIENT_STORAGE]){super(e,t.INSUFFICIENT_STORAGE,!0)}},y=E=>E instanceof T&&E.isOparational?{status:"error",statusCode:E.statusCode,message:E.message,code:E.code}:{status:"error",statusCode:t.INTERNAL_SERVER_ERROR,message:s[t.INTERNAL_SERVER_ERROR],code:m(t.INTERNAL_SERVER_ERROR)},j=(E,e,R,o)=>{let O=y(E);R.status(O.statusCode).json({status:O.status,statusCode:O.statusCode,message:O.message,code:O.code})};var w=(E=[])=>E[E.length-1],v=Function.prototype;function k(E,e){return Object.keys(E).forEach(R=>{e[R]=E[R]}),e}function h(E){let e=function(...o){let O=E.apply(this,o),r=(o.length===5?o[2]:w(o))||v;return O&&O.catch&&O.catch(B=>r(B)),O};return Object.defineProperty(e,"length",{value:E.length,writable:!1}),k(E,e)}function Z(){let E=Y.default.prototype.constructor.param;Y.default.prototype.constructor.param=function(R,o){return o=h(o),E.call(this,R,o)}}Object.defineProperty(Q.default.prototype,"handle",{enumerable:!0,get(){return this.__handle},set(E){E=h(E),this.__handle=E}});Z();0&&(module.exports={AppError,BadGatewayError,BadRequestError,ConflictError,DefaultStatusMessages,ForbiddenError,GatewayTimeoutError,GoneError,HttpStatusCodes,InsufficientStorageError,MethodNotAllowedError,NotAcceptableError,NotFoundError,NotImplementedError,PayloadTooLargeError,PaymentRequiredError,PreconditionFailedError,RequestTimeoutError,ServerError,ServiceUnavailableError,TooManyRequestsError,UnauthorizedError,UnprocessableEntityError,UnsupportedMediaTypeError,globalErrorHandler,handleError}); //# sourceMappingURL=index.js.map