UNPKG

neat-catch

Version:

A comprehensive utility library for elegant error handling in TypeScript and JavaScript. Handle both sync and async operations with clean tuple-based results.

2 lines (1 loc) 2.79 kB
function m(n,e){let s=t=>[null,c(t,e)];try{let t=n();return t instanceof Promise||t!=null&&(typeof t=="object"||typeof t=="function")&&typeof t.then=="function"&&typeof t.catch=="function"?t.then(a=>[a,null]).catch(s):[t,null]}catch(t){return s(t)}}function w(n,e){return(...s)=>m(()=>n(...s),e)}async function k(n,e){let s=await Promise.allSettled(n.map(o=>m(o,e))),t=[],a=[];return s.forEach((o,u)=>{if(o.status==="fulfilled"){let[r,i]=o.value;i?a[u]=i:t[u]=r}else a[u]=c(o.reason,e)}),{results:t.length?t:null,errors:a.length?a:null}}async function d(n,e={}){let{maxRetries:s=3,delay:t=1e3,backoff:a="exponential",errorTransformer:o,shouldRetry:u=()=>!0}=e;for(let r=1;r<=s+1;r++){let[i,l]=await m(n);if(!l)return[i,null];let f=r===s+1,g=!f&&u(l,r);if(!g)return[null,o?c(l,p=>o(p,r)):l];let E=a==="exponential"?t*Math.pow(2,r-1):t*r;console.log({attempt:r,isLastAttempt:f,shouldContinue:g,nextDelay:E}),await new Promise(T=>setTimeout(T,E))}return[null,new Error("Unexpected retry loop exit")]}var y={toString:n=>n instanceof Error?n.message:typeof n=="string"?n:n&&typeof n=="object"&&"message"in n&&typeof n.message=="string"?n.message:String(n),toObject:n=>n instanceof Error?{message:n.message,stack:n.stack,name:n.name,...n.cause!==void 0&&{cause:n.cause}}:n&&typeof n=="object"?{message:n.message||"Unknown error",..."stack"in n&&typeof n.stack=="string"&&{stack:n.stack},..."name"in n&&typeof n.name=="string"&&{name:n.name},..."cause"in n&&{cause:n.cause}}:{message:String(n)},withTimestamp:n=>{let e=Date.now();return{error:n,timestamp:e,isoString:new Date(e).toISOString()}},fetchError:n=>{if(n&&typeof n=="object"&&"status"in n)return{message:`HTTP error ${n.status}`,status:Number(n.status),statusText:"statusText"in n?String(n.statusText):void 0,url:"url"in n?String(n.url):void 0,isNetworkError:!1,isTimeout:Number(n.status)===408};if(n instanceof TypeError){let e=n.message.toLowerCase();return{message:n.message,isNetworkError:e.includes("network")||e.includes("fetch")||e.includes("request"),isTimeout:e.includes("timeout")}}return n instanceof Error&&n.name==="TimeoutError"?{message:n.message,isNetworkError:!0,isTimeout:!0}:n instanceof Error?{message:n.message,isNetworkError:!1,isTimeout:!1}:{message:String(n),isNetworkError:!1,isTimeout:!1}},withContext:n=>e=>({...n,error:e}),toSimpleError:n=>({message:n instanceof Error?n.message:String(n)}),forLogging:n=>({...y.toObject(n),timestamp:Date.now(),...typeof process<"u"&&process.env&&process.env.NODE_ENV&&{environment:process.env.NODE_ENV}})},c=(n,e)=>{try{return e?e(n):n}catch(s){return new AggregateError([n,s],"Error transforming error. Both errors are in the 'errors' property.")}},x=m;export{w as createNeatWrapper,x as default,y as errorTransformers,m as neatCatch,k as neatCatchAll,d as neatCatchRetry};