alibaba / hooks

A high-quality & reliable React Hooks library. https://ahooks.pages.dev/
https://ahooks.js.org/
MIT License
14.06k stars 2.71k forks source link

Bug: Promise.reject('useRequest has caught the exception...) should not be invoked when throwOnError is false or undefined #812

Closed KrishnaPG closed 2 years ago

KrishnaPG commented 3 years ago

When the useRequest is used as part of the umi this below code causes the application to crash due to unhandled promise rejections: https://github.com/alibaba/hooks/blob/fd6d0a442cc8d569912cb2be6a8d68d882127ce5/packages/use-request/src/useAsync.ts#L172-L175

In a umi based application, the app.tsx contains a default error handler that shows error alert notifications before the useRequest hook's _run catch handler is invoked. That is, error is already handled hence when throwOnError is not true, the _run method should not reject the promise and instead just set the error state.

  const { data, error, loading } = useRequest(getData, {   throwOnError: false  });

Problem:

Solution:

The promise rejection is creating a catch-22 loop that is impossible to break. For more details, please refer to: https://github.com/umijs/umi/issues/5910

brickspert commented 3 years ago

I knew this problem long ago.. We add promise.reject when throwOnError: false, just for run().catch()

Someone need run to rejected when the request is error.

They think no matter what throwOnError is, run should can catch the error...

brickspert commented 3 years ago

I cannot remove the promise.reject, because it's a break change..

I will fix this on next major version.

mzvast commented 3 years ago

So how is the bug now?

brickspert commented 2 years ago

Hello:

这个问题我们已经在 v3.0 版本彻底修复,目前 v3.0 版本已经发布 alpha 版本。

欢迎试用。

文档:https://ahooks-next.surge.sh/ 源码:https://github.com/alibaba/hooks/tree/next

安装最新分支

$ npm install --save ahooks@next
# or
$ yarn add ahooks@next

We have fixed this problem in the v3.0 version, and the alpha has been released.

Welcome to try.

Doc:https://ahooks-next.surge.sh/ Source Code:https://github.com/alibaba/hooks/tree/next

Install v3.0

$ npm install --save ahooks@next
# or
$ yarn add ahooks@next