86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame^] | 1 | import type { RequestOptions } from '@@/plugin-request/request'; |
| 2 | import type { RequestConfig } from '@umijs/max'; |
| 3 | import { message, notification } from 'antd'; |
| 4 | |
| 5 | // 错误处理方案: 错误类型 |
| 6 | enum ErrorShowType { |
| 7 | SILENT = 0, |
| 8 | WARN_MESSAGE = 1, |
| 9 | ERROR_MESSAGE = 2, |
| 10 | NOTIFICATION = 3, |
| 11 | REDIRECT = 9, |
| 12 | } |
| 13 | // 与后端约定的响应数据格式 |
| 14 | interface ResponseStructure { |
| 15 | success: boolean; |
| 16 | data: any; |
| 17 | errorCode?: number; |
| 18 | errorMessage?: string; |
| 19 | showType?: ErrorShowType; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @name 错误处理 |
| 24 | * pro 自带的错误处理, 可以在这里做自己的改动 |
| 25 | * @doc https://umijs.org/docs/max/request#配置 |
| 26 | */ |
| 27 | export const errorConfig: RequestConfig = { |
| 28 | // 错误处理: umi@3 的错误处理方案。 |
| 29 | errorConfig: { |
| 30 | // 错误抛出 |
| 31 | errorThrower: (res) => { |
| 32 | const { success, data, errorCode, errorMessage, showType } = |
| 33 | res as unknown as ResponseStructure; |
| 34 | if (!success) { |
| 35 | const error: any = new Error(errorMessage); |
| 36 | error.name = 'BizError'; |
| 37 | error.info = { errorCode, errorMessage, showType, data }; |
| 38 | throw error; // 抛出自制的错误 |
| 39 | } |
| 40 | }, |
| 41 | // 错误接收及处理 |
| 42 | errorHandler: (error: any, opts: any) => { |
| 43 | if (opts?.skipErrorHandler) throw error; |
| 44 | // 我们的 errorThrower 抛出的错误。 |
| 45 | if (error.name === 'BizError') { |
| 46 | const errorInfo: ResponseStructure | undefined = error.info; |
| 47 | if (errorInfo) { |
| 48 | const { errorMessage, errorCode } = errorInfo; |
| 49 | switch (errorInfo.showType) { |
| 50 | case ErrorShowType.SILENT: |
| 51 | // do nothing |
| 52 | break; |
| 53 | case ErrorShowType.WARN_MESSAGE: |
| 54 | message.warning(errorMessage); |
| 55 | break; |
| 56 | case ErrorShowType.ERROR_MESSAGE: |
| 57 | message.error(errorMessage); |
| 58 | break; |
| 59 | case ErrorShowType.NOTIFICATION: |
| 60 | notification.open({ |
| 61 | description: errorMessage, |
| 62 | message: errorCode, |
| 63 | }); |
| 64 | break; |
| 65 | case ErrorShowType.REDIRECT: |
| 66 | // TODO: redirect |
| 67 | break; |
| 68 | default: |
| 69 | message.error(errorMessage); |
| 70 | } |
| 71 | } |
| 72 | } else if (error.response) { |
| 73 | // Axios 的错误 |
| 74 | // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围 |
| 75 | message.error(`Response status:${error.response.status}`); |
| 76 | } else if (error.request) { |
| 77 | // 请求已经成功发起,但没有收到响应 |
| 78 | // \`error.request\` 在浏览器中是 XMLHttpRequest 的实例, |
| 79 | // 而在node.js中是 http.ClientRequest 的实例 |
| 80 | message.error('None response! Please retry.'); |
| 81 | } else { |
| 82 | // 发送请求时出了点问题 |
| 83 | message.error('Request error, please retry.'); |
| 84 | } |
| 85 | }, |
| 86 | }, |
| 87 | |
| 88 | // 请求拦截器 |
| 89 | requestInterceptors: [ |
| 90 | (config: RequestOptions) => { |
| 91 | // 拦截请求配置,进行个性化处理。 |
| 92 | const url = config?.url?.concat('?token = 123'); |
| 93 | return { ...config, url }; |
| 94 | }, |
| 95 | ], |
| 96 | |
| 97 | // 响应拦截器 |
| 98 | responseInterceptors: [ |
| 99 | (response) => { |
| 100 | // 拦截响应数据,进行个性化处理 |
| 101 | const { data } = response as unknown as ResponseStructure; |
| 102 | |
| 103 | if (data?.success === false) { |
| 104 | message.error('请求失败!'); |
| 105 | } |
| 106 | return response; |
| 107 | }, |
| 108 | ], |
| 109 | }; |