fix: 配置了catchError的请求,失败时没执行统一的errorCallback

This commit is contained in:
ivinwu
2021-04-15 16:20:31 +08:00
parent 91490a495a
commit ef2c1515f9
6 changed files with 27 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
import { IRequestOption, IUploadFileOption } from "../interface";
declare type ThrowErrorType = 'upload-error' | 'logic-error' | 'http-error';
declare type ThrowErrorType = 'logic-error' | 'http-error';
interface ThrowError {
type: ThrowErrorType;
res: any;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
{
"name": "we-request",
"version": "1.3.0",
"version": "1.3.1",
"description": "本工具通过拓展小程序的wx.request让开发者通过简单的配置实现自动管理登录态等功能",
"keywords": [
"登录态",

View File

@@ -1,18 +1,23 @@
import { IRequestOption, IUploadFileOption } from "../interface";
import errorHandler from "./errorHandler";
import config from '../store/config'
type ThrowErrorType = 'upload-error' | 'logic-error' | 'http-error'
type ThrowErrorType = 'logic-error' | 'http-error'
interface ThrowError {
type: ThrowErrorType
res: any
}
function catchHandler(e: ThrowError, obj: IRequestOption | IUploadFileOption, reject: (reason?: any) => void) {
const { type, res } = e
const { type, res } = e;
// 如果有配置统一错误回调函数,则执行它(仅逻辑错误)
if (typeof config.errorCallback === "function" && type === 'logic-error') {
config.errorCallback(obj, res);
}
if (obj.catchError) {
if (type === 'http-error') {
return reject(new Error(res.statusCode.toString()));
} else if (type === 'upload-error') {
return reject(new Error(res));
} else if (type === 'logic-error') {
let msg = errorHandler.getErrorMsg(res);
return reject(new Error(msg.content));
@@ -21,13 +26,13 @@ function catchHandler(e: ThrowError, obj: IRequestOption | IUploadFileOption, re
return reject(e);
}
} else {
if (e.type) {
return errorHandler.logicError(obj, e.res);
if (type) {
return errorHandler.logicError(obj, res);
} else {
// 其他js错误
return reject(e);
}
}
}
export { catchHandler }

View File

@@ -21,11 +21,6 @@ function logicError(obj: IRequestOption | IUploadFileOption, res: wx.RequestSucc
const retry = () => request(obj).then(obj._resolve).catch(obj._reject);
doError(title, content, retry);
}
// 如果有配置统一错误回调函数,则执行它
if (typeof config.errorCallback === "function") {
config.errorCallback(obj, res);
}
}
function getErrorMsg(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult) {