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.36 kB
var p=Object.defineProperty;var s=(t,r)=>p(t,"name",{value:r,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 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"}}s(d,"detectRuntime");function c(t){let r=d(),n=t.replace(/\r\n/g,` `),e=n.split(` `),o=[],i=0;for(let a of e){if(!a.trim().startsWith("at ")){o.push(a);continue}if(!l.some(f=>f.test(a))){if(i>=50){o.push(" ... (truncated)");break}o.push(a),i++}}return n=o.join(` `),(r.type==="browser"||r.type==="bun")&&(n=g(n)),n}s(c,"normalizeStack");function g(t){if(typeof globalThis<"u"&&"__stackTraceMapper"in globalThis)try{let r=globalThis.__stackTraceMapper;if(typeof r=="function")return r(t)||t}catch{}return t}s(g,"applySourceMaps");var u=class t extends Error{static{s(this,"AppError")}code;cause;data;status;tags;severity;retryable;id;timestamp;constructor(r,n,e={}){super(n||r),this.name="AppError",this.code=r,this.cause=e.cause,this.data=e.data,this.tags=Object.freeze(e.tags||[]),this.severity=e.severity||"error",this.status=e.status,this.retryable=e.retryable??!1,this.id=m(),this.timestamp=Date.now(),e.captureStack!==!1&&(Error.captureStackTrace&&Error.captureStackTrace(this,t),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 m(){let t=Date.now(),r=new Uint8Array(10);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(r);else for(let i=0;i<10;i++)r[i]=Math.floor(Math.random()*256);let n=s((i,a)=>i.toString(16).padStart(a,"0"),"hex"),e=Math.floor(t/4294967296),o=t&4294967295;return[n(e,8),n(o>>>16&65535,4),n(o&65535&4095|28672,4),n((r[0]<<8|r[1])&16383|32768,4),n(r[2]<<24|r[3]<<16|r[4]<<8|r[5],8)+n(r[6]<<8|r[7],4)].join("-")}s(m,"generateUUIDv7");function y(t){let r={type:`urn:error:${t.code}`,title:t.code,status:t.status,detail:t.message,instance:t.id};return t.severity&&(r.severity=t.severity),t.retryable&&(r.retryable=t.retryable),t.tags.length>0&&(r.tags=t.tags),t.data!==void 0&&(r.data=t.data),t.timestamp&&(r.timestamp=t.timestamp),r}s(y,"toProblem");function h(t){let r=b(t.type)||t.title||"unknown/error",n=t.detail||t.title,e={status:t.status};return typeof t.severity=="string"&&(e.severity=t.severity),typeof t.retryable=="boolean"&&(e.retryable=t.retryable),Array.isArray(t.tags)&&(e.tags=t.tags),t.data!==void 0&&(e.data=t.data),new u(r,n,e)}s(h,"fromProblem");function b(t){if(t){if(t.startsWith("urn:error:"))return t.slice(10);if(t.startsWith("http://")||t.startsWith("https://")){let r=t.split("/");return r[r.length-1]}return t}}s(b,"extractCodeFromType");function O(t,r){return JSON.stringify(y(t),null,r)}s(O,"toProblemJSON");function M(t){let r=JSON.parse(t);return h(r)}s(M,"fromProblemJSON");export{h as fromProblem,M as fromProblemJSON,y as toProblem,O as toProblemJSON};