UNPKG

@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.72 kB
"use strict";var p=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var n=(r,e)=>p(r,"name",{value:e,configurable:!0});var w=(r,e)=>{for(var t in e)p(r,t,{get:e[t],enumerable:!0})},y=(r,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of d(e))!m.call(r,o)&&o!==t&&p(r,o,{get:()=>e[o],enumerable:!(s=k(e,o))||s.enumerable});return r};var R=r=>y(p({},"__esModule",{value:!0}),r);var z={};w(z,{ResultWrapper:()=>l,err:()=>h,isErr:()=>W,isOk:()=>S,map:()=>P,mapErr:()=>C,ok:()=>f,safeAwait:()=>F,safeFunc:()=>A,unwrap:()=>O,unwrapErr:()=>M,unwrapOr:()=>j});module.exports=R(z);var g=[/at Object\.<anonymous>/,/at Module\._compile/,/at Module\.load/,/at Function\.Module\._load/,/at Module\.require/,/at require \(node:internal/,/node_modules/,/@fajarnugraha37\/error\/dist/];function v(){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(v,"detectRuntime");function E(r){let e=v(),t=r.replace(/\r\n/g,` `),s=t.split(` `),o=[],u=0;for(let i of s){if(!i.trim().startsWith("at ")){o.push(i);continue}if(!g.some(T=>T.test(i))){if(u>=50){o.push(" ... (truncated)");break}o.push(i),u++}}return t=o.join(` `),(e.type==="browser"||e.type==="bun")&&(t=b(t)),t}n(E,"normalizeStack");function b(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(b,"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=x(),this.timestamp=Date.now(),s.captureStack!==!1&&(Error.captureStackTrace&&Error.captureStackTrace(this,r),this.stack&&(this.stack=E(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 c(r){if(U(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(c,"fromUnknown");function U(r){return r instanceof a}n(U,"isAppError");function x(){let r=Date.now(),e=new Uint8Array(10);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(e);else for(let u=0;u<10;u++)e[u]=Math.floor(Math.random()*256);let t=n((u,i)=>u.toString(16).padStart(i,"0"),"hex"),s=Math.floor(r/4294967296),o=r&4294967295;return[t(s,8),t(o>>>16&65535,4),t(o&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(x,"generateUUIDv7");function f(r){return new l({ok:!0,value:r,error:null})}n(f,"ok");function h(r){return new l({ok:!1,value:null,error:r})}n(h,"err");var l=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:c(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 F(r){try{let e=await r;return f(e)}catch(e){return h(c(e))}}n(F,"safeAwait");function A(r,...e){try{let t=r(...e);return f(t)}catch(t){return h(c(t))}}n(A,"safeFunc");function S(r){return r.ok===!0}n(S,"isOk");function W(r){return r.ok===!1}n(W,"isErr");function O(r){if(r.ok)return r.value;throw r.error}n(O,"unwrap");function M(r){if(!r.ok)return r.error;throw new Error("Called unwrapErr on an Ok result")}n(M,"unwrapErr");function j(r,e){return r.ok?r.value:e}n(j,"unwrapOr");function P(r,e){return r.ok?{ok:!0,value:e(r.value),error:null}:r}n(P,"map");function C(r,e){return r.ok?r:{ok:!1,value:null,error:e(r.error)}}n(C,"mapErr");0&&(module.exports={ResultWrapper,err,isErr,isOk,map,mapErr,ok,safeAwait,safeFunc,unwrap,unwrapErr,unwrapOr});