my-uniapp-tools
Version:
一个简洁稳定的 uni-app 开发工具库,提供剪贴板、本地存储、导航、系统信息等常用功能
2 lines (1 loc) • 1.43 kB
JavaScript
export{VERSION}from"./version.js";class r 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 e{static instance;errorCallbacks=[];static getInstance(){return e.instance||(e.instance=new e),e.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(e,t){const n=e instanceof Error?e:new Error(String(e)),s={code:n instanceof r?n.code:"UNKNOWN_ERROR",message:n.message,module:n instanceof r?n.module:t,timestamp:n instanceof r?n.timestamp:Date.now(),stack:n.stack};console.error(`[${s.module}] ${s.code}: ${s.message}`,s),this.errorCallbacks.forEach(r=>{try{r(s)}catch(r){console.error("Error in error callback:",r)}})}createModuleErrorHandler(e){return(t,n,s)=>{const o=new r(t,n,e);return s instanceof Error&&(o.stack=s.stack),this.handleError(o,e),o}}}async function t(r,t,n="ASYNC_ERROR"){try{return await r()}catch(r){return e.getInstance().createModuleErrorHandler(t)(n,`异步操作失败: ${r instanceof Error?r.message:String(r)}`,r),null}}function n(r,t,n="SYNC_ERROR",s=null){try{return r()}catch(r){return e.getInstance().createModuleErrorHandler(t)(n,`同步操作失败: ${r instanceof Error?r.message:String(r)}`,r),s}}export{e as ErrorHandler,r as UniAppToolsError,t as safeAsync,n as safeSync};