alibaba / hooks

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

UseAPIProvider全局错误配置问题 #415

Closed xiexingen closed 4 years ago

xiexingen commented 4 years ago

经测试发现如下问题

  1. 1.9.2版本的时候 全局配置过UseAPIProvider的onError来进行全局的异常处理 image 当在实际页面调用的时候,如下代码所示 run(values, id).then(() => { handleBack(); }); 这种形式,个人感觉 如果异常了不应该再继续执行到then(目前是异常了还是会进入到then里面) 断点调试截图: image image

  2. 1.9.3版本里面 全局配置的UseAPIProvider里面的onError压根就不生效了

个人观点 1.配置全局onError后,如果具体页面调用的时候没有则走的全局的onError 2.如果页面级别使用useRequest的时候配置了onError,那么走的是页面里面配置的onError 3.当走了onError的时候,不应该还能执行到then里面去

brickspert commented 4 years ago

1.配置全局onError后,如果具体页面调用的时候没有则走的全局的onError

目前就是这样的

2.如果页面级别使用useRequest的时候配置了onError,那么走的是页面里面配置的onError

目前就是这样的

3.当走了onError的时候,不应该还能执行到then里面去

run().catch().then() useRequest 内部 catch 了异常,但是 catch 自己会返回一个 Promise,触发 then。 如果我们手动 return Promise.reject() ,可能是一个 break change~

xiexingen commented 4 years ago

image 我这边跑出来的结果确实是这样的

xiexingen commented 4 years ago

codesandbox不咋会玩 建了一个demo,打了几个debug 可以在调试模式下看步骤 代码地址: https://codesandbox.io/s/github/xiexingen/demo-umi-hooks/tree/master/?file=/src/demo.tsx image

brickspert commented 4 years ago

你说的是对的,我需要想下怎么解决的。

xiexingen commented 4 years ago

看到有个throwOnError 是不是通过配置throwOnError:true来实现这个

brickspert commented 4 years ago

看到有个throwOnError 是不是通过配置throwOnError:true来实现这个

  1. 可以配置 throwOnError,这时候会正常 throw error
  2. 另外,昨天提交的 PR #456 ,哪怕不配置 throwOnError,也不会触发 run().then() 了
xiexingen commented 4 years ago

坐等第二种形式的包,到时候我再试试

brickspert commented 4 years ago

已发布。

xiexingen commented 4 years ago

收到 试过了 蛮香的 有个其他的问题 阿里源居然没找到2.1.0版本

KrishnaPG commented 3 years ago

I am using it as below and do not want to handle the errors manually. What is the solution?

I am getting the error: ...Uncaught (in promise) useRequest has caught the exception...

  import { useRequest } from 'umi';

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

  if (error)
    return <div>Failed to Load Data</div>;
  if (loading) {
    return <div>loading...</div>;
  }
  return <div>Username: {data}</div>;

If I use throwOnError: true how to catch the error (I am not using manual:true, so run is not available).

Ideally useRequest should take care of the error handling (notification alert) and should not complain a warning when throwOnError: false

xiexingen commented 3 years ago

I am using it as below and do not want to handle the errors manually. What is the solution?

I am getting the error: ...Uncaught (in promise) useRequest has caught the exception...

  import { useRequest } from 'umi';

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

  if (error)
    return <div>Failed to Load Data</div>;
  if (loading) {
    return <div>loading...</div>;
  }
  return <div>Username: {data}</div>;

If I use throwOnError: true how to catch the error (I am not using manual:true, so run is not available).

Ideally useRequest should take care of the error handling (notification alert) and should not complain a warning when throwOnError: false

I think should pass onError function useRequest(getData,{ onError:()=>{ // TODO something } })

KrishnaPG commented 3 years ago

Thank you @xiexingen The onError does not help when the promise is rejected. More details are here: https://github.com/alibaba/hooks/issues/812