UNPKG

@byloth/exceptions

Version:

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

163 lines (154 loc) 5.57 kB
var f = Object.defineProperty; var u = (a, e, t) => e in a ? f(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t; var h = (a, e, t) => u(a, typeof e != "symbol" ? e + "" : e, t); var p = Object.defineProperty, y = (a, e, t) => e in a ? p(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t, w = (a, e, t) => y(a, typeof e != "symbol" ? e + "" : e, t), r; typeof process < "u" && ((r = process.versions) != null && r.node); var l; typeof self == "object" && ((l = self.constructor) == null || l.name); var o, d; class s extends (d = Error, o = Symbol.toStringTag, d) { /** * Initializes a new instance of the {@link Exception} class. * * ```ts * throw new Exception("An error occurred while processing the request."); * ``` * * @param message The message that describes the error. * @param cause The previous caught error that caused this one, if any. * @param name The name of the exception. Default is `"Exception"`. */ constructor(e, t, n = "Exception") { super(e), w(this, o, "Exception"), this.cause = t, this.name = n, t && (t instanceof Error ? this.stack += ` Caused by ${t.stack}` : this.stack += ` Caused by ${t}`); } /** * A static method to convert a generic caught error, ensuring it's an instance of the {@link Exception} class. * * ```ts * try { [...] } * catch (error) * { * const exc = Exception.FromUnknown(error); * * [...] * } * ``` * * @param error The caught error to convert. * * @returns An instance of the {@link Exception} class. */ 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 _ extends s { constructor(t, n, c = "HandledException") { n === void 0 && (t instanceof s ? n = "The exception has been handled properly." : n = "The error has been handled properly."); super(n); h(this, "handled"); this.name = c, t instanceof Error ? this.stack += ` [Handled]${t.stack}` : this.stack += ` [Handled]${t}`, this.handled = t; } } class i { constructor(e = {}) { h(this, "_options"); h(this, "_handlers"); h(this, "_catch"); h(this, "_catchSet"); h(this, "_default"); h(this, "_defaultSet"); this._options = { ...i.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((n) => this._handlers.push({ type: n, 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: n } of this._handlers) if (e instanceof t) return n(e); return e instanceof _ ? 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 S(a, e, t, n) { const c = new i(); return t || (t = (b) => { }), c.on(e, t), n && c.default(n), c.handle(a); } function g(a, e) { const t = new i(); return t.ignore(e), t.handle(a); } const k = "2.3.1"; export { _ as HandledException, i as HandlerBuilder, k as VERSION, S as expect, g as ignore }; //# sourceMappingURL=exceptions.js.map