tryless
Version:
Type-safe error handling for TypeScript without try-catch hell
3 lines (2 loc) • 2.98 kB
JavaScript
const r="unknown",t="unwrap",n="undefined"!=typeof require;const e=function(){if(n)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,n,e,s){let u=e||"Could not unwrap result";u+=` '${s||("error"in r?r.error:t)}'`;const o={};"captureStackTrace"in Error&&"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(o,n),u+=o.stack?.replace("Error\n","\n"),"stack"in r&&(u+=` ${r.stack?.replace("Error\n","\n\nError created at\n")}`),super(u),this.stack=o.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(n&&e){const r={...t,depth:null===t.depth?null:t.depth-1};s+=`\n\nReason ${e(this.reason,r)}`}else try{s+=`\n\nReason ${JSON.stringify(this.reason,null,2)}`}catch{s+=`\n\nReason ${String(this.reason)}`}return s}}class u{constructor(r){this.success=r}isOk(){return!0===this.success}isErr(){return!1===this.success}}class o extends u{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 u{constructor(r,t,n){super(!1),"captureStackTrace"in Error&&"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,n||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 o(r)}function c(t=r,n=void 0){return new i(t,n,c)}function h(r){return t=>a(r(t))}function p(r){return function t(n){return new i(r,n,t)}}function f(t,n){const e=n??r;if(t instanceof Promise)return t.then(a,p(e));if("function"!=typeof t)throw new Error("fn must be a function or a promise");return function r(...n){try{const s=t(...n);return s instanceof Promise?s.then(a,t=>new i(e,t,r)):a(s)}catch(t){return new i(e,t,r)}}}export{i as Err,o as Ok,u as Result,r as UnknownError,s as UnwrapError,t as UnwrapErrorName,c as err,p as errReject,a as ok,h as okFulfilled,f as resultfy};
//# sourceMappingURL=index.esm.js.map