UNPKG

my-uniapp-tools

Version:

一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能

2 lines (1 loc) 1.47 kB
"use strict";var r=require("./version.cjs");class e extends Error{code;module;timestamp;constructor(r,e,t){super(e),this.name="UniAppToolsError",this.code=r,this.module=t,this.timestamp=Date.now()}}class t{static instance;errorCallbacks=[];static getInstance(){return t.instance||(t.instance=new t),t.instance}onError(r){return"function"!=typeof r?()=>{}:(this.errorCallbacks.push(r),()=>{const e=this.errorCallbacks.indexOf(r);e>=0&&this.errorCallbacks.splice(e,1)})}handleError(r,t){const s=r instanceof Error?r:new Error(String(r)),n={code:s instanceof e?s.code:"UNKNOWN_ERROR",message:s.message,module:s instanceof e?s.module:t,timestamp:s instanceof e?s.timestamp:Date.now(),stack:s.stack};console.error(`[${n.module}] ${n.code}: ${n.message}`,n),this.errorCallbacks.forEach(r=>{try{r(n)}catch(r){console.error("Error in error callback:",r)}})}createModuleErrorHandler(r){return(t,s,n)=>{const o=new e(t,s,r);return n instanceof Error&&(o.stack=n.stack),this.handleError(o,r),o}}}exports.VERSION=r.VERSION,exports.ErrorHandler=t,exports.UniAppToolsError=e,exports.safeAsync=async function(r,e,s="ASYNC_ERROR"){try{return await r()}catch(r){return t.getInstance().createModuleErrorHandler(e)(s,`异步操作失败: ${r instanceof Error?r.message:String(r)}`,r),null}},exports.safeSync=function(r,e,s="SYNC_ERROR",n=null){try{return r()}catch(r){return t.getInstance().createModuleErrorHandler(e)(s,`同步操作失败: ${r instanceof Error?r.message:String(r)}`,r),n}};