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