From 71fa0bdd9dfa41c08e66f9f50f1eda221ce20df1 Mon Sep 17 00:00:00 2001 From: ivinwu Date: Wed, 19 Apr 2023 11:38:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E6=8F=90=E7=A4=BA=E6=96=87=E6=A1=88?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9EsystemErrorHandler=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E8=87=AA=E8=A1=8C=E5=A4=84=E7=90=86=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interface.ts | 2 ++ src/module/errorHandler.ts | 4 +++- src/store/config.ts | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/interface.ts b/src/interface.ts index 6af4330..3c0f53c 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -54,6 +54,8 @@ export interface IInitOption { errorHandler?: Function | null; /* 请求发送前,提供hook给开发者自定义修改发送内容 */ beforeSend?: Function | null; + /* 自定义系统错误处理函数 */ + systemErrorHandler?: Function | null; } export interface ICodeToSessionOptions{ diff --git a/src/module/errorHandler.ts b/src/module/errorHandler.ts index 849f17b..4688a0c 100644 --- a/src/module/errorHandler.ts +++ b/src/module/errorHandler.ts @@ -5,9 +5,11 @@ import { IRequestOption, IUploadFileOption } from "../interface"; function systemError(obj: IRequestOption | IUploadFileOption, res: WechatMiniprogram.GeneralCallbackResult) { if (typeof obj.fail === "function") { obj.fail(res); + } else if (typeof config.systemErrorHandler === 'function') { + config.systemErrorHandler(res); } else { const retry = () => request(obj).then(obj._resolve).catch(obj._reject); - doError("", res.errMsg, retry); + doError("", "", retry); } } diff --git a/src/store/config.ts b/src/store/config.ts index fb8f74d..821a653 100644 --- a/src/store/config.ts +++ b/src/store/config.ts @@ -30,7 +30,9 @@ const defaultConfig: IInitOption = { // 自定义错误处理函数 errorHandler: null, // 请求发送前,提供hook给开发者自定义修改发送内容 - beforeSend: null + beforeSend: null, + // 自定义系统错误处理函数(网络错误) + systemErrorHandler: null, }; export default defaultConfig;