UNPKG

@byloth/exceptions

Version:

Handle exceptions with ease, create better stacktraces and manage everything in the right place. ❌

19 lines (10 loc) 4.19 kB
(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.Exceptions={}))})(this,function(n){"use strict";var g=Object.defineProperty;var x=(n,a,o)=>a in n?g(n,a,{enumerable:!0,configurable:!0,writable:!0,value:o}):n[a]=o;var c=(n,a,o)=>x(n,typeof a!="symbol"?a+"":a,o);var a=Object.defineProperty,o=(i,e,t)=>e in i?a(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,w=(i,e,t)=>o(i,typeof e!="symbol"?e+"":e,t),r;typeof process<"u"&&((r=process.versions)!=null&&r.node);var f;typeof self=="object"&&((f=self.constructor)==null||f.name);var u,p;class s extends(p=Error,u=Symbol.toStringTag,p){constructor(e,t,h="Exception"){super(e),w(this,u,"Exception"),this.cause=t,this.name=h,t&&(t instanceof Error?this.stack+=` Caused by ${t.stack}`:this.stack+=` Caused by ${t}`)}static FromUnknown(e){if(e instanceof s)return e;if(e instanceof Error){const t=new s(e.message);return t.stack=e.stack,t.name=e.name,t}return new s(`${e}`)}}class y extends s{constructor(t,h,l="HandledException"){h===void 0&&(t instanceof s?h="The exception has been handled properly.":h="The error has been handled properly.");super(h);c(this,"handled");this.name=l,t instanceof Error?this.stack+=` [Handled]${t.stack}`:this.stack+=` [Handled]${t}`,this.handled=t}}class d{constructor(e={}){c(this,"_options");c(this,"_handlers");c(this,"_catch");c(this,"_catchSet");c(this,"_default");c(this,"_defaultSet");this._options={...d.DefaultOpts,...e},this._handlers=[],this._catch=t=>{throw t},this._catchSet=!1,this._default=t=>{throw t},this._defaultSet=!1}static get DefaultOpts(){return{rethrowHandled:!1}}on(e,t){if(this._catchSet)throw new s("The catch handler has already been set. You cannot specify a new exception type to handle after the catch handler has been set.");if(this._defaultSet)throw new s("The default handler has already been set. You cannot specify a new exception type to handle after the default handler has been set.");return Array.isArray(e)?e.forEach(h=>this._handlers.push({type:h,handler:t})):this._handlers.push({type:e,handler:t}),this}ignore(e){if(this._catchSet)throw new s("The catch handler has already been set. You cannot ignore an exception type after the catch handler has been set.");if(this._defaultSet)throw new s("The default handler has already been set. You cannot ignore an exception type after the default handler has been set.");return Array.isArray(e)?e.forEach(t=>this._handlers.push({type:t,handler:()=>{}})):this._handlers.push({type:e,handler:()=>{}}),this}catch(e){if(this._catchSet)throw new s("The catch handler has already been set. You cannot specify more than one catch handler.");return this._catch=e,this._catchSet=!0,this}default(e){if(this._catchSet)throw new s("The catch handler has already been set. You cannot specify a default handler after the catch handler has been set.");if(this._defaultSet)throw new s("The default handler has already been set. You cannot specify more than one default handler.");return this._default=e,this._defaultSet=!0,this}handle(e){try{if(this._options.rethrowHandled&&this._handlers.length===0)return console.warn("Handling an exception this way is redundant and causes some execution overhead.\nDid you maybe miss using the `on` method to define the exception type to handle?"),this._default(e);for(const{type:t,handler:h}of this._handlers)if(e instanceof t)return h(e);return e instanceof y?console.warn(e):this._default(e)}catch(t){return t instanceof Error?e instanceof Error?t.stack+=` Has occurred while trying to handle ${e.stack}`:t.stack+=` Has occurred while trying to handle ${e}`:e instanceof Error?t=`${t} Has occurred while trying to handle ${e.stack}`:t=`${t} Has occurred while trying to handle ${e}`,this._catch(t)}}}function b(i,e,t,h){const l=new d;return t||(t=E=>{}),l.on(e,t),h&&l.default(h),l.handle(i)}function _(i,e){const t=new d;return t.ignore(e),t.handle(i)}const S="2.3.1";n.HandledException=y,n.HandlerBuilder=d,n.VERSION=S,n.expect=b,n.ignore=_,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=exceptions.umd.cjs.map