@fajarnugraha37/error
Version:
Runtime-agnostic error handling library with structured errors, adapters, and validation support for Bun, Node.js, and browsers
5 lines (4 loc) • 5.17 kB
JavaScript
var T=Object.defineProperty;var n=(r,e)=>T(r,"name",{value:e,configurable:!0});var k=[/at Object\.<anonymous>/,/at Module\._compile/,/at Module\.load/,/at Function\.Module\._load/,/at Module\.require/,/at require \(node:internal/,/node_modules/,/\/error\/dist/];function d(){return typeof globalThis<"u"&&"Bun"in globalThis?{type:"bun",version:globalThis.Bun?.version}:typeof process<"u"&&process.versions?.node?{type:"node",version:process.versions.node}:typeof window<"u"?{type:"browser"}:{type:"unknown"}}n(d,"detectRuntime");function c(r){let e=d(),t=r.replace(/\r\n/g,`
`),s=t.split(`
`),u=[],o=0;for(let i of s){if(!i.trim().startsWith("at ")){u.push(i);continue}if(!k.some(E=>E.test(i))){if(o>=50){u.push(" ... (truncated)");break}u.push(i),o++}}return t=u.join(`
`),(e.type==="browser"||e.type==="bun")&&(t=m(t)),t}n(c,"normalizeStack");function m(r){if(typeof globalThis<"u"&&"__stackTraceMapper"in globalThis)try{let e=globalThis.__stackTraceMapper;if(typeof e=="function")return e(r)||r}catch{}return r}n(m,"applySourceMaps");var a=class r extends Error{static{n(this,"AppError")}code;cause;data;status;tags;severity;retryable;id;timestamp;constructor(e,t,s={}){super(t||e),this.name="AppError",this.code=e,this.cause=s.cause,this.data=s.data,this.tags=Object.freeze(s.tags||[]),this.severity=s.severity||"error",this.status=s.status,this.retryable=s.retryable??!1,this.id=y(),this.timestamp=Date.now(),s.captureStack!==!1&&(Error.captureStackTrace&&Error.captureStackTrace(this,r),this.stack&&(this.stack=c(this.stack))),Object.freeze(this)}toJSON(){return{name:this.name,code:this.code,message:this.message,severity:this.severity,status:this.status,retryable:this.retryable,id:this.id,timestamp:this.timestamp,tags:this.tags,data:this.data,stack:this.stack,cause:this.cause instanceof Error?this.cause.message:this.cause}}};function l(r){if(w(r))return r;if(r instanceof Error)return new a("error/from-unknown",r.message,{cause:r,data:{originalName:r.name}});if(typeof r=="string")return new a("error/from-unknown",r);if(typeof r=="object"&&r!==null){let e=r,t=String(e.message||e.error||"Unknown error");return new a("error/from-unknown",t,{data:e})}return new a("error/from-unknown",String(r),{data:{originalValue:r}})}n(l,"fromUnknown");function w(r){return r instanceof a}n(w,"isAppError");function y(){let r=Date.now(),e=new Uint8Array(10);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(e);else for(let o=0;o<10;o++)e[o]=Math.floor(Math.random()*256);let t=n((o,i)=>o.toString(16).padStart(i,"0"),"hex"),s=Math.floor(r/4294967296),u=r&4294967295;return[t(s,8),t(u>>>16&65535,4),t(u&65535&4095|28672,4),t((e[0]<<8|e[1])&16383|32768,4),t(e[2]<<24|e[3]<<16|e[4]<<8|e[5],8)+t(e[6]<<8|e[7],4)].join("-")}n(y,"generateUUIDv7");function f(r){return new p({ok:!0,value:r,error:null})}n(f,"ok");function h(r){return new p({ok:!1,value:null,error:r})}n(h,"err");var p=class r{constructor(e){this.result=e}static{n(this,"ResultWrapper")}unwrapResult(){return this.result}isOk(){return this.result.ok===!0}isErr(){return this.result.ok===!1}map(e){return this.result.ok?new r({ok:!0,value:e(this.result.value),error:null}):new r(this.result)}async mapAsync(e){if(this.result.ok)try{let t=await e(this.result.value);return new r({ok:!0,value:t,error:null})}catch(t){return new r({ok:!1,value:null,error:l(t)})}return new r(this.result)}mapErr(e){return this.result.ok?new r(this.result):new r({ok:!1,value:null,error:e(this.result.error)})}flatMap(e){return this.result.ok?e(this.result.value):new r(this.result)}tap(e){return this.result.ok&&e(this.result.value),this}tapErr(e){return this.result.ok||e(this.result.error),this}unwrap(){if(this.result.ok)return this.result.value;throw this.result.error}unwrapErr(){if(!this.result.ok)return this.result.error;throw new Error("Called unwrapErr on an Ok result")}unwrapOr(e){return this.result.ok?this.result.value:e}unwrapOrElse(e){return this.result.ok?this.result.value:e(this.result.error)}match(e){return this.result.ok?e.ok(this.result.value):e.err(this.result.error)}toPromise(){return this.result.ok?Promise.resolve(this.result.value):Promise.reject(this.result.error)}and(e){return this.result.ok&&e.result.ok?new r({ok:!0,value:[this.result.value,e.result.value],error:null}):this.result.ok?new r(e.result):new r(this.result)}or(e){return this.result.ok?this:e}};async function A(r){try{let e=await r;return f(e)}catch(e){return h(l(e))}}n(A,"safeAwait");function S(r,...e){try{let t=r(...e);return f(t)}catch(t){return h(l(t))}}n(S,"safeFunc");function W(r){return r.ok===!0}n(W,"isOk");function O(r){return r.ok===!1}n(O,"isErr");function M(r){if(r.ok)return r.value;throw r.error}n(M,"unwrap");function j(r){if(!r.ok)return r.error;throw new Error("Called unwrapErr on an Ok result")}n(j,"unwrapErr");function P(r,e){return r.ok?r.value:e}n(P,"unwrapOr");function C(r,e){return r.ok?{ok:!0,value:e(r.value),error:null}:r}n(C,"map");function z(r,e){return r.ok?r:{ok:!1,value:null,error:e(r.error)}}n(z,"mapErr");export{p as ResultWrapper,h as err,O as isErr,W as isOk,C as map,z as mapErr,f as ok,A as safeAwait,S as safeFunc,M as unwrap,j as unwrapErr,P as unwrapOr};