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.49 kB
"use strict";var c=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var s=(r,e)=>c(r,"name",{value:e,configurable:!0});var h=(r,e)=>{for(var n in e)c(r,n,{get:e[n],enumerable:!0})},w=(r,e,n,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of g(e))!y.call(r,o)&&o!==n&&c(r,o,{get:()=>e[o],enumerable:!(t=m(e,o))||t.enumerable});return r};var b=r=>w(c({},"__esModule",{value:!0}),r);var S={};h(S,{match:()=>A});module.exports=b(S);var k=[/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 E(){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(E,"detectRuntime");function f(r){let e=E(),n=r.replace(/\r\n/g,` `),t=n.split(` `),o=[],i=0;for(let u of t){if(!u.trim().startsWith("at ")){o.push(u);continue}if(!k.some(l=>l.test(u))){if(i>=50){o.push(" ... (truncated)");break}o.push(u),i++}}return n=o.join(` `),(e.type==="browser"||e.type==="bun")&&(n=T(n)),n}s(f,"normalizeStack");function T(r){if(typeof globalThis<"u"&&"__stackTraceMapper"in globalThis)try{let e=globalThis.__stackTraceMapper;if(typeof e=="function")return e(r)||r}catch{}return r}s(T,"applySourceMaps");var a=class r extends Error{static{s(this,"AppError")}code;cause;data;status;tags;severity;retryable;id;timestamp;constructor(e,n,t={}){super(n||e),this.name="AppError",this.code=e,this.cause=t.cause,this.data=t.data,this.tags=Object.freeze(t.tags||[]),this.severity=t.severity||"error",this.status=t.status,this.retryable=t.retryable??!1,this.id=x(),this.timestamp=Date.now(),t.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 d(r){if(p(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}})}s(d,"fromUnknown");function p(r){return r instanceof a}s(p,"isAppError");function x(){let r=Date.now(),e=new Uint8Array(10);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(e);else for(let i=0;i<10;i++)e[i]=Math.floor(Math.random()*256);let n=s((i,u)=>i.toString(16).padStart(u,"0"),"hex"),t=Math.floor(r/4294967296),o=r&4294967295;return[n(t,8),n(o>>>16&65535,4),n(o&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("-")}s(x,"generateUUIDv7");function A(r,e){if(!p(r)){if(e._)return e._(d(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)}s(A,"match");0&&(module.exports={match});