UNPKG

tryless

Version:

Type-safe error handling for TypeScript without try-catch hell

3 lines (2 loc) 3.03 kB
"use strict";const r="unknown",t="unwrap",e="undefined"!=typeof require;const n=function(){if(e)try{if("undefined"!=typeof require){const r=require("util");if("inspect"in r&&"function"==typeof r.inspect)return r.inspect}return}catch{return}}();class s extends Error{constructor(r,e,n,s){let o=n||"Could not unwrap result";o+=` '${s||("error"in r?r.error:t)}'`;const u={};"captureStackTrace"in Error&&"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(u,e),o+=u.stack?.replace("Error\n","\n"),"stack"in r&&(o+=` ${r.stack?.replace("Error\n","\n\nError created at\n")}`),super(o),this.stack=u.stack,this.error=s||("error"in r?r.error:t),"reason"in r&&(this.reason=r.reason),this.name="UnwrapError"}[Symbol.for("nodejs.util.inspect.custom")](r,t){if(r<0)return this.message;let s=this.message;if(this.reason)if(e&&n){const r={...t,depth:null===t.depth?null:t.depth-1};s+=`\n\nReason ${n(this.reason,r)}`}else try{s+=`\n\nReason ${JSON.stringify(this.reason,null,2)}`}catch{s+=`\n\nReason ${String(this.reason)}`}return s}}class o{constructor(r){this.success=r}isOk(){return!0===this.success}isErr(){return!1===this.success}}class u extends o{constructor(r){super(!0),this.data=r}unwrap(){return this.data}unwrapOr(r){return this.data}unwrapOrElse(r){return this.data}unwrapErr(r){throw new s(this,this.unwrapErr,"Could not unwrap error",r)}unwrapErrOr(r){return r}unwrapErrOrElse(r){return r(this)}expect(r,t){throw new s(this,this.expect,`Expected error ${r}, but got success`,t)}and(r){return r}andThen(r){return r(this.data)}or(r){return this}orElse(r){return this}map(r){return r(this)}toString(){return`Success<${this.data}>`}}class i extends o{constructor(r,t,e){super(!1),"captureStackTrace"in Error&&"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,e||this.constructor),this.error=r,this.reason=t}unwrap(r){throw new s(this,this.unwrap,"Could not unwrap error",r)}unwrapOr(r){return r}unwrapOrElse(r){return r(this)}unwrapErr(){return this}unwrapErrOr(r){return this}unwrapErrOrElse(r){return this}expect(r,t){if(this.error===r)return this;throw new s(this,this.expect,`Expected error ${r}, but got error ${this.error}`,t)}and(r){return this}andThen(r){return this}or(r){return r}orElse(r){return r(this)}map(r){return r(this)}toString(){return`${this.error}: ${this.reason} - ${this.stack}`}}function a(r=void 0){return new u(r)}function c(r){return function t(e){return new i(r,e,t)}}exports.Err=i,exports.Ok=u,exports.Result=o,exports.UnknownError=r,exports.UnwrapError=s,exports.UnwrapErrorName=t,exports.err=function t(e=r,n=void 0){return new i(e,n,t)},exports.errReject=c,exports.ok=a,exports.okFulfilled=function(r){return t=>a(r(t))},exports.resultfy=function(t,e){const n=e??r;if(t instanceof Promise)return t.then(a,c(n));if("function"!=typeof t)throw new Error("fn must be a function or a promise");return function r(...e){try{const s=t(...e);return s instanceof Promise?s.then(a,t=>new i(n,t,r)):a(s)}catch(t){return new i(n,t,r)}}}; //# sourceMappingURL=index.cjs.js.map