umi icon indicating copy to clipboard operation
umi copied to clipboard

修复执行errorHandler后还继续抛出error

Open MoLinJie opened this issue 1 year ago • 10 comments

https://github.com/umijs/umi/issues/10670#issue-1607817828

MoLinJie avatar Jul 04 '23 10:07 MoLinJie

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
umi ⬜️ Ignored (Inspect) Jul 4, 2023 10:22am

vercel[bot] avatar Jul 04 '23 10:07 vercel[bot]

Codecov Report

Patch and project coverage have no change.

Comparison is base (f3668f5) 29.03% compared to head (451b721) 29.03%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #11360   +/-   ##
=======================================
  Coverage   29.03%   29.03%           
=======================================
  Files         484      484           
  Lines       14667    14667           
  Branches     3460     3460           
=======================================
  Hits         4259     4259           
  Misses       9657     9657           
  Partials      751      751           
Impacted Files Coverage Δ
packages/plugins/src/request.ts 0.00% <ø> (ø)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov[bot] avatar Jul 04 '23 10:07 codecov[bot]

@fz6m 什么时候处理呢

MoLinJie avatar Aug 04 '23 03:08 MoLinJie

坐等合并

yuu2lee4 avatar Oct 24 '23 14:10 yuu2lee4

我注意到 https://github.com/umijs/umi/discussions/11831#discussioncomment-7482863 回复所述,在 umi 3 中同样即使配置了 errorHandler 还会抛出错误,这应该是预期一致的行为吧?

fz6m avatar Nov 06 '23 02:11 fz6m

mark。

什么时候合并呀

biezhihua avatar Nov 17 '23 14:11 biezhihua

跪求merge

chj-damon avatar Dec 15 '23 06:12 chj-damon

如果在 umi 3 中具有同样继续抛出 error 的行为,则该变化属于 Breaking change ,会影响较多用户,在没有充足的解决方式前暂不考虑继续跟进。

fz6m avatar Dec 15 '23 09:12 fz6m

@fz6m umi3为什么不会报这个uncaught error?

chj-damon avatar Dec 18 '23 02:12 chj-damon

自定义错误处理


export default {
  baseURL: (REACT_APP_ENV === 'prod' && PROD_BASE_API_URL_API_URL) || '',
  timeout: 30000,
  // `validateStatus` defines whether to resolve or reject the promise for a given
  // HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
  // or `undefined`), the promise will be resolved; otherwise, the promise will be
  // rejected.
  validateStatus: function (status: number) {
    // return true;
    return status >= 200 && status < 300; // default
  },
  requestInterceptors: [],
  responseInterceptors: [
    [
      (response: AxiosResponse) => {
        return response;
      },
      (error: AxiosError) => {
        /**
         * 说明: 对接服务端API
         * API方案:
         * 1、错误定义对接服务端的API,规定业务错误响应状态码400,登录的401
         * 2、其他状态码包括:500、404等一律按照非业务正常响应
         *
         * 服务端响应:
         * 1、服务端响应体的内容统一为:{data,code,message},code同步为状态码
         * 2、预定 PostBlogApi 为提交日志的接口
         *
         * umi request 处理
         * 1、响应状态码正确时时(<300 && >=200),返回 Promise.resolve(response),
         * 会执行 PostBlogApi.then(res),res为 response.data
         *
         * 2、响应状态为错误时(>=200 && <300),返回 Promise.reject(response?.data),
         * 会执行 PostBlogApi.catch(res),res为 response.data,所以可以把业务错误放在data
         * 里面,供业务逻辑消遣
         *
         * 建议:既然约定了400为业务错误返回,那应该确保服务端返回的业务错误必须为400,不要搞特殊返回
         */

        console.log('响应错误: ', error);
        const { response } = error;

        if (response) {
          const data: any = response?.data;
          if (response.status === 400) {
            return Promise.reject(error);
          } else if (response.status === 401) {
            history.push(LOGIN_PATH);
          } else if (response?.data && typeof response.data === 'string') {
            message.error(response.data);
          } else if (data?.message) {
            message.error(data.message);
          } else {
            message.error(response.statusText);
          }
          return Promise.reject(error);
        } else {
          message.error('响应失败');
          console.log('error not found response: ');
          return Promise.reject(error);
        }
      },
    ],
  ],
};

forwzb avatar Aug 13 '24 07:08 forwzb