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