UNPKG

@fajarnugraha37/error

Version:

Runtime-agnostic error handling library with structured errors, adapters, and validation support for Bun, Node.js, and browsers

6 lines (5 loc) 3.82 kB
var d=Object.defineProperty;var a=(n,e)=>d(n,"name",{value:e,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"}}a(m,"detectRuntime");function p(n){let e=m(),t=n.replace(/\r\n/g,` `),r=t.split(` `),s=[],o=0;for(let i of r){if(!i.trim().startsWith("at ")){s.push(i);continue}if(!l.some(f=>f.test(i))){if(o>=50){s.push(" ... (truncated)");break}s.push(i),o++}}return t=s.join(` `),(e.type==="browser"||e.type==="bun")&&(t=h(t)),t}a(p,"normalizeStack");function h(n){if(typeof globalThis<"u"&&"__stackTraceMapper"in globalThis)try{let e=globalThis.__stackTraceMapper;if(typeof e=="function")return e(n)||n}catch{}return n}a(h,"applySourceMaps");var u=class n extends Error{static{a(this,"AppError")}code;cause;data;status;tags;severity;retryable;id;timestamp;constructor(e,t,r={}){super(t||e),this.name="AppError",this.code=e,this.cause=r.cause,this.data=r.data,this.tags=Object.freeze(r.tags||[]),this.severity=r.severity||"error",this.status=r.status,this.retryable=r.retryable??!1,this.id=g(),this.timestamp=Date.now(),r.captureStack!==!1&&(Error.captureStackTrace&&Error.captureStackTrace(this,n),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 g(){let n=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=a((o,i)=>o.toString(16).padStart(i,"0"),"hex"),r=Math.floor(n/4294967296),s=n&4294967295;return[t(r,8),t(s>>>16&65535,4),t(s&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("-")}a(g,"generateUUIDv7");function c(n,e){let t=`Validation failed with ${n.length} issue(s)`;return new u("validation/failed",t,{...e,data:{issues:n},status:e?.status??400})}a(c,"makeValidationError");function S(n){if(!n||!Array.isArray(n.issues))throw new Error("Invalid Zod error object");let e=n.issues.map(t=>({path:t.path||[],code:t.code||"invalid",message:t.message||"Validation failed",meta:{expected:t.expected,received:t.received}}));return c(e)}a(S,"fromZodError");function V(n){if(!Array.isArray(n))throw new Error("Invalid TypeBox errors array");let e=n.map(t=>({path:t.path?t.path.split("/").filter(Boolean):[],code:t.type||"invalid",message:t.message||"Validation failed",meta:{schema:t.schema,value:t.value}}));return c(e)}a(V,"fromTypeboxError");function v(n){if(!Array.isArray(n))throw new Error("Invalid AJV errors array");let e=n.map(t=>({path:t.instancePath?t.instancePath.split("/").filter(Boolean):[],code:t.keyword||"invalid",message:t.message||"Validation failed",meta:{params:t.params,schema:t.schema,parentSchema:t.parentSchema}}));return c(e)}a(v,"fromAjvError");function T(n){let e=new Set,t=[];for(let r of n){let s=`${r.path.join("/")}:${r.code}`;e.has(s)||(e.add(s),t.push(r))}return t}a(T,"deduplicateIssues");function I(n){return n.map(e=>` - ${e.path.length>0?e.path.join("."):"(root)"}: ${e.message} [${e.code}]`).join(` `)}a(I,"formatValidationIssues");export{T as deduplicateIssues,I as formatValidationIssues,v as fromAjvError,V as fromTypeboxError,S as fromZodError,c as makeValidationError};