@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) • 3.03 kB
JavaScript
var l=Object.defineProperty;var t=(r,e)=>l(r,"name",{value:e,configurable:!0});var m=[/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 g(){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"}}t(g,"detectRuntime");function p(r){let e=g(),n=r.replace(/\r\n/g,`
`),o=n.split(`
`),i=[],s=0;for(let u of o){if(!u.trim().startsWith("at ")){i.push(u);continue}if(!m.some(d=>d.test(u))){if(s>=50){i.push(" ... (truncated)");break}i.push(u),s++}}return n=i.join(`
`),(e.type==="browser"||e.type==="bun")&&(n=y(n)),n}t(p,"normalizeStack");function y(r){if(typeof globalThis<"u"&&"__stackTraceMapper"in globalThis)try{let e=globalThis.__stackTraceMapper;if(typeof e=="function")return e(r)||r}catch{}return r}t(y,"applySourceMaps");var a=class r extends Error{static{t(this,"AppError")}code;cause;data;status;tags;severity;retryable;id;timestamp;constructor(e,n,o={}){super(n||e),this.name="AppError",this.code=e,this.cause=o.cause,this.data=o.data,this.tags=Object.freeze(o.tags||[]),this.severity=o.severity||"error",this.status=o.status,this.retryable=o.retryable??!1,this.id=h(),this.timestamp=Date.now(),o.captureStack!==!1&&(Error.captureStackTrace&&Error.captureStackTrace(this,r),this.stack&&(this.stack=p(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 f(r){if(c(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,n=String(e.message||e.error||"Unknown error");return new a("error/from-unknown",n,{data:e})}return new a("error/from-unknown",String(r),{data:{originalValue:r}})}t(f,"fromUnknown");function c(r){return r instanceof a}t(c,"isAppError");function h(){let r=Date.now(),e=new Uint8Array(10);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(e);else for(let s=0;s<10;s++)e[s]=Math.floor(Math.random()*256);let n=t((s,u)=>s.toString(16).padStart(u,"0"),"hex"),o=Math.floor(r/4294967296),i=r&4294967295;return[n(o,8),n(i>>>16&65535,4),n(i&65535&4095|28672,4),n((e[0]<<8|e[1])&16383|32768,4),n(e[2]<<24|e[3]<<16|e[4]<<8|e[5],8)+n(e[6]<<8|e[7],4)].join("-")}t(h,"generateUUIDv7");function S(r,e){if(!c(r)){if(e._)return e._(f(r));throw new Error("match() requires AppError or a fallback handler '_'")}let n=e[r.code]||e._;if(!n)throw new Error(`No handler found for error code: ${r.code}`);return n(r)}t(S,"match");export{S as match};