umijs / umi

A framework in react community ✨
https://umijs.org
MIT License
15.3k stars 2.65k forks source link

useRequest如何捕获异常? #5212

Closed sussertod891015 closed 4 years ago

sussertod891015 commented 4 years ago

使用useRequest请求接口,抛出了异常,但是捕获不到

最小可复现仓库

devScripts.js:5836 BizError: 产品Id不能为空 at _callee$ (http://localhost:8002/umi.js:160735:23) at tryCatch (http://localhost:8002/umi.js:58549:40) at Generator.invoke [as _invoke] (http://localhost:8002/umi.js:58778:22) at Generator.prototype. [as next] (http://localhost:8002/umi.js:58601:21) at asyncGeneratorStep (http://localhost:8002/umi.js:56037:24) at _next (http://localhost:8002/umi.js:56059:9)

我想问下该如何捕获这个异常

相关环境信息

xiexingen commented 4 years ago

我是这样处理的,没用umi的request 在app文件中rootContainer中配置ahooks的错误拦截,页面级别如果没有catch和配置onError会走全局的,页面级别有的话 会走页面级别的 可以参考下 https://github.com/wetrial/wetrial-template/blob/master/src/app.tsx

holiday12138 commented 4 years ago

+1 拿不到error 都是undefined

const { data, loading, error } = useRequest(() => {
    return getFilters();
  });
KellenHe commented 4 years ago

为什么收到的data是undefined。

xiaohuoni commented 4 years ago

看看这个文档 https://ahooks.js.org/zh-CN/hooks/async#result