From 6f4028bb7dabde78e87e49d56ab214d09d9ccc32 Mon Sep 17 00:00:00 2001 From: bac-joker Date: Mon, 12 Apr 2021 11:29:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=9F=E4=B8=80request=20=E6=89=80?= =?UTF-8?q?=E6=9C=89=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/reference/plugin/plugins/request.md | 4 ++-- .../src/template/resErrorProcess.js | 24 ++++++++++++------- packages/fes-template-h5/src/app.js | 7 ++++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/zh/reference/plugin/plugins/request.md b/docs/zh/reference/plugin/plugins/request.md index 91306232..777ad34e 100644 --- a/docs/zh/reference/plugin/plugins/request.md +++ b/docs/zh/reference/plugin/plugins/request.md @@ -65,8 +65,8 @@ export const request = { 404(error) { }, - commonAbnormalCodeHandler(response) { - // 非 0,且非 11199(前面已经配置过 11199) code 其他异常code处理逻辑 + default(error) { + // 异常统一处理 } }, // 其他 axios 配置 diff --git a/packages/fes-plugin-request/src/template/resErrorProcess.js b/packages/fes-plugin-request/src/template/resErrorProcess.js index 067034b3..d0f6db42 100644 --- a/packages/fes-plugin-request/src/template/resErrorProcess.js +++ b/packages/fes-plugin-request/src/template/resErrorProcess.js @@ -2,10 +2,22 @@ import { isObject } from './helpers'; function handleAbnormalCode(errorHandler = {}, code, response) { if (errorHandler[code]) { - errorHandler[code](response); - } else if (errorHandler.commonAbnormalCodeHandler) { + errorHandler[code](response.data); + } else if (errorHandler.default) { // 处理其他异常 - errorHandler.commonAbnormalCodeHandler(response); + errorHandler.default({ + response + }); + } +} + +function handleRequestError(errorHandler = {}, error) { + if (error.type) { + errorHandler[error.type] && errorHandler[error.type](error); + } else if (error.response) { + errorHandler[error.response.status] && errorHandler[error.response.status](error); + } else if (errorHandler.default) { + errorHandler.default(error); } } @@ -22,11 +34,7 @@ export default async (ctx, next) => { ctx.error = response; // code 不为零进入 reject } } else if (error) { - if (error.type) { - errorHandler[error.type] && errorHandler[error.type](error); - } else if (error.response) { - errorHandler[error.response.status] && errorHandler[error.response.status](error); - } + handleRequestError(errorHandler, error); } await next(); diff --git a/packages/fes-template-h5/src/app.js b/packages/fes-template-h5/src/app.js index db279236..f5074050 100644 --- a/packages/fes-template-h5/src/app.js +++ b/packages/fes-template-h5/src/app.js @@ -1,10 +1,13 @@ export const request = { errorHandler: { + 111(responseData) { + console.log(responseData); + }, 404() { console.log('to 404 page'); }, - commonAbnormalCodeHandler(res) { - console.log(res.data.code); + default(error) { + console.log(error.response.data); } } };