Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MoLinJie
Copy link

@MoLinJie MoLinJie commented Jul 4, 2023

@vercel
Copy link

vercel bot commented Jul 4, 2023

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

@codecov
Copy link

codecov bot commented Jul 4, 2023

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% <ø> (ø)

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

@MoLinJie
Copy link
Author

MoLinJie commented Aug 4, 2023

@fz6m 什么时候处理呢

@yuu2lee4
Copy link

坐等合并

@fz6m
Copy link
Contributor

fz6m commented Nov 6, 2023

我注意到 #11831 (reply in thread) 回复所述,在 umi 3 中同样即使配置了 errorHandler 还会抛出错误,这应该是预期一致的行为吧?

@biezhihua
Copy link

mark。

什么时候合并呀

@chj-damon
Copy link
Contributor

跪求merge

@fz6m
Copy link
Contributor

fz6m commented Dec 15, 2023

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

@chj-damon
Copy link
Contributor

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

@forwzb
Copy link

forwzb commented Aug 13, 2024

自定义错误处理


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);
        }
      },
    ],
  ],
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants