requestErrorConfig.ts 3.3 KB

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