fix(requestHandler): 修改请求或网络异常时的处理逻辑,避免promise.then中的代码继续执行 (#30)
* fix(requestHandler): 修复请求或网络异常时处理后的response为undefined导致promise.then继续执行的问题 当请求未触发成功条件或statusCode不为200时,responseHandler返回的response为undefined,导致resolve返回并执行promise.then中的代码. 目前通过判断response是否为undefined决定是否resolve(response)来避免break change * fix(request&&uploadFile): 修改responseHandler抛出异常的逻辑并增加捕获异常的处理luoji
This commit is contained in:
8
build/module/catchHandler.d.ts
vendored
Normal file
8
build/module/catchHandler.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare type ThrowErrorType = 'upload-error' | 'logic-error' | 'http-error';
|
||||
interface ThrowError {
|
||||
type: ThrowErrorType;
|
||||
res: any;
|
||||
}
|
||||
declare function catchHandler(e: ThrowError, obj: IRequestOption | IUploadFileOption, reject: (reason?: any) => void): void;
|
||||
export { catchHandler };
|
||||
File diff suppressed because one or more lines are too long
2
build/weRequest.min.js
vendored
2
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
25
src/module/catchHandler.ts
Normal file
25
src/module/catchHandler.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
import errorHandler from "./errorHandler";
|
||||
|
||||
type ThrowErrorType = 'upload-error' | '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
|
||||
if (obj.catchError) {
|
||||
if (type === 'http-error') {
|
||||
reject(new Error(res.statusCode.toString()));
|
||||
} else if (type === 'upload-error') {
|
||||
reject(new Error(res));
|
||||
} else if (type === 'logic-error') {
|
||||
let msg = errorHandler.getErrorMsg(res);
|
||||
reject(new Error(msg.content));
|
||||
}
|
||||
} else {
|
||||
errorHandler.logicError(obj, e.res);
|
||||
}
|
||||
|
||||
}
|
||||
export { catchHandler }
|
||||
@@ -7,8 +7,9 @@ import sessionManager from './sessionManager'
|
||||
import responseHandler from './responseHandler'
|
||||
import durationReporter from "./durationReporter"
|
||||
import url from '../util/url'
|
||||
import {IRequestOption, IUploadFileOption} from "../interface"
|
||||
import { IRequestOption, IUploadFileOption } from "../interface"
|
||||
import errorHandler from "./errorHandler";
|
||||
import { catchHandler } from './catchHandler';
|
||||
|
||||
// 格式化url
|
||||
function format(originUrl: string) {
|
||||
@@ -55,12 +56,12 @@ function initializeRequestObj(obj: IRequestOption) {
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj.data = {...obj.data as object, [config.sessionName]: status.session};
|
||||
obj.data = { ...obj.data as object, [config.sessionName]: status.session };
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
const gd = getGlobalData();
|
||||
obj.data = {...gd, ...obj.data as object};
|
||||
obj.data = { ...gd, ...obj.data as object };
|
||||
|
||||
obj.method = obj.method || 'GET';
|
||||
obj.dataType = obj.dataType || 'json';
|
||||
@@ -68,7 +69,7 @@ function initializeRequestObj(obj: IRequestOption) {
|
||||
// 如果请求不是GET,则在URL中自动加上登录态和全局参数
|
||||
if (!config.doNotUseQueryString && obj.method !== "GET") {
|
||||
if (status.session) {
|
||||
obj.url = url.setParams(obj.url, {[config.sessionName]: status.session});
|
||||
obj.url = url.setParams(obj.url, { [config.sessionName]: status.session });
|
||||
}
|
||||
obj.url = url.setParams(obj.url, gd);
|
||||
}
|
||||
@@ -85,17 +86,17 @@ function initializeUploadFileObj(obj: IUploadFileOption) {
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj.formData = {...obj.formData as object, [config.sessionName]: status.session};
|
||||
obj.formData = { ...obj.formData as object, [config.sessionName]: status.session };
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
const gd = getGlobalData();
|
||||
obj.formData = {...gd, ...obj.formData};
|
||||
obj.formData = { ...gd, ...obj.formData };
|
||||
|
||||
if (!config.doNotUseQueryString) {
|
||||
// 将登陆态也带在url上
|
||||
if (status.session) {
|
||||
obj.url = url.setParams(obj.url, {[config.sessionName]: status.session});
|
||||
obj.url = url.setParams(obj.url, { [config.sessionName]: status.session });
|
||||
}
|
||||
// 全局参数同时放在url上
|
||||
obj.url = url.setParams(obj.url, gd);
|
||||
@@ -191,9 +192,11 @@ function request(obj: IRequestOption): any {
|
||||
return doRequest(obj)
|
||||
}).then((res) => {
|
||||
let response = responseHandler(res as wx.RequestSuccessCallbackResult, obj, 'request');
|
||||
return resolve(response);
|
||||
if (response != null) {
|
||||
return resolve(response);
|
||||
}
|
||||
}).catch((e) => {
|
||||
return reject(e);
|
||||
catchHandler(e, obj, reject)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -214,9 +217,11 @@ function uploadFile(obj: IUploadFileOption): any {
|
||||
return doUploadFile(obj)
|
||||
}).then((res) => {
|
||||
let response = responseHandler(res as wx.UploadFileSuccessCallbackResult, obj, 'uploadFile');
|
||||
return resolve(response);
|
||||
if (response != null) {
|
||||
return resolve(response);
|
||||
}
|
||||
}).catch((e) => {
|
||||
return reject(e);
|
||||
catchHandler(e, obj, reject)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,16 +14,11 @@ function response(
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
// 兼容uploadFile返回的res.data可能是字符串
|
||||
if(typeof res.data === "string") {
|
||||
if (typeof res.data === "string") {
|
||||
try {
|
||||
res.data = JSON.parse(res.data);
|
||||
} catch (e) {
|
||||
if(obj.catchError) {
|
||||
throw new Error(e);
|
||||
} else {
|
||||
errorHandler.logicError(obj, res);
|
||||
return;
|
||||
}
|
||||
throw { type: 'upload-error', res: e };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,9 +27,9 @@ function response(
|
||||
if (config.loginTrigger!(res.data) && obj.reLoginCount !== undefined && obj.reLoginCount < config.reLoginLimit!) {
|
||||
// 登录态失效,且重试次数不超过配置
|
||||
sessionManager.delSession();
|
||||
if(method === "request") {
|
||||
if (method === "request") {
|
||||
return requestHandler.request(obj as IRequestOption);
|
||||
} else if(method === "uploadFile") {
|
||||
} else if (method === "uploadFile") {
|
||||
return requestHandler.uploadFile(obj as IUploadFileOption);
|
||||
}
|
||||
} else if (config.successTrigger(res.data)) {
|
||||
@@ -49,9 +44,9 @@ function response(
|
||||
} catch (e) {
|
||||
console.error("Function successData occur error: " + e);
|
||||
}
|
||||
if(!(obj as IRequestOption).noCacheFlash) {
|
||||
if (!(obj as IRequestOption).noCacheFlash) {
|
||||
// 如果为了保证页面不闪烁,则不回调,只是缓存最新数据,待下次进入再用
|
||||
if(typeof obj.success === "function"){
|
||||
if (typeof obj.success === "function") {
|
||||
obj.success(realData);
|
||||
} else {
|
||||
return realData;
|
||||
@@ -61,20 +56,11 @@ function response(
|
||||
cacheManager.set(obj, realData);
|
||||
} else {
|
||||
// 接口返回失败码
|
||||
if(obj.catchError) {
|
||||
let msg = errorHandler.getErrorMsg(res);
|
||||
throw new Error(msg.content);
|
||||
} else {
|
||||
errorHandler.logicError(obj, res);
|
||||
}
|
||||
throw { type: 'logic-error', res }
|
||||
}
|
||||
} else {
|
||||
// https返回状态码非200
|
||||
if(obj.catchError) {
|
||||
throw new Error(res.statusCode.toString());
|
||||
} else {
|
||||
errorHandler.logicError(obj, res);
|
||||
}
|
||||
throw { type: 'http-error', res }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user