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.87 kB
"use strict";var u=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var s=(t,r)=>u(t,"name",{value:r,configurable:!0});var h=(t,r)=>{for(var e in r)u(t,e,{get:r[e],enumerable:!0})},b=(t,r,e,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of m(r))!y.call(t,i)&&i!==e&&u(t,i,{get:()=>r[i],enumerable:!(n=g(r,i))||n.enumerable});return t};var w=t=>b(u({},"__esModule",{value:!0}),t);var M={};h(M,{fromProblem:()=>l,fromProblemJSON:()=>O,toProblem:()=>p,toProblemJSON:()=>T});module.exports=w(M);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 A(){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(A,"detectRuntime");function f(t){let r=A(),e=t.replace(/\r\n/g,` `),n=e.split(` `),i=[],o=0;for(let a of n){if(!a.trim().startsWith("at ")){i.push(a);continue}if(!k.some(d=>d.test(a))){if(o>=50){i.push(" ... (truncated)");break}i.push(a),o++}}return e=i.join(` `),(r.type==="browser"||r.type==="bun")&&(e=E(e)),e}s(f,"normalizeStack");function E(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(E,"applySourceMaps");var c=class t extends Error{static{s(this,"AppError")}code;cause;data;status;tags;severity;retryable;id;timestamp;constructor(r,e,n={}){super(e||r),this.name="AppError",this.code=r,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=S(),this.timestamp=Date.now(),n.captureStack!==!1&&(Error.captureStackTrace&&Error.captureStackTrace(this,t),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(){let t=Date.now(),r=new Uint8Array(10);if(typeof crypto<"u"&&crypto.getRandomValues)crypto.getRandomValues(r);else for(let o=0;o<10;o++)r[o]=Math.floor(Math.random()*256);let e=s((o,a)=>o.toString(16).padStart(a,"0"),"hex"),n=Math.floor(t/4294967296),i=t&4294967295;return[e(n,8),e(i>>>16&65535,4),e(i&65535&4095|28672,4),e((r[0]<<8|r[1])&16383|32768,4),e(r[2]<<24|r[3]<<16|r[4]<<8|r[5],8)+e(r[6]<<8|r[7],4)].join("-")}s(S,"generateUUIDv7");function p(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(p,"toProblem");function l(t){let r=x(t.type)||t.title||"unknown/error",e=t.detail||t.title,n={status:t.status};return typeof t.severity=="string"&&(n.severity=t.severity),typeof t.retryable=="boolean"&&(n.retryable=t.retryable),Array.isArray(t.tags)&&(n.tags=t.tags),t.data!==void 0&&(n.data=t.data),new c(r,e,n)}s(l,"fromProblem");function x(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(x,"extractCodeFromType");function T(t,r){return JSON.stringify(p(t),null,r)}s(T,"toProblemJSON");function O(t){let r=JSON.parse(t);return l(r)}s(O,"fromProblemJSON");0&&(module.exports={fromProblem,fromProblemJSON,toProblem,toProblemJSON});