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

全局错误在哪捕获? #2756

Closed
musicode opened this issue Jul 2, 2018 · 11 comments
Closed

全局错误在哪捕获? #2756

musicode opened this issue Jul 2, 2018 · 11 comments

Comments

@musicode
Copy link

musicode commented Jul 2, 2018

我看了 异常处理 的文档,但没找到一个想要的捕获全局错误的方法。

安装 egg-onerror 之后,在 config.default.js 里加了 onerror 也没调用,很无奈。

文档是否可以写的更清楚些?

@egg-bot
Copy link

egg-bot commented Jul 2, 2018

Translation of this issue:


Where is the global error captured?

I looked at the documentation for [Exception Handling] (https://eggjs.org/zh-cn/core/error-handling.html) but didn't find a way to capture global errors.

After installing egg-onerror, adding onerror to config.default.js is also not called, very helpless.

Can the document be written more clearly?

@egg-bot
Copy link

egg-bot commented Jul 2, 2018

Hello @musicode. Please provide a reproducible example by creating a github repo.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.

@atian25
Copy link
Member

atian25 commented Jul 2, 2018

我也很无奈,issue 能否写的更清楚一些呢?不是有 template 提示必须的内容么?

@musicode
Copy link
Author

musicode commented Jul 2, 2018

这个 issue 不是具体的 bug,我觉得应该没必要写环境版本之类的吧?

我处理错误的方式是 ctx.app.emit('error', err, ctx),这样 egg 会自动写到 error 日志里,但是我希望能拦截处理下,比如发个报警短信啥的,但我没在文档里找到应该怎么实现。

@atian25
Copy link
Member

atian25 commented Jul 2, 2018

  1. 如果可以,请使用 egg-init --type=simple bug 提供一个最小可复现的代码仓库,方便我们排查问题。
  1. egg-onerror 是自带的,无需自行安装
  2. 你看它的文档,有配置处理函数的,如果不生效,99% 的可能是你配置错了,所以才要求提供可复现方式。
  3. 既然写在 README 了,肯定是支持的啦,覆盖率都在那,看下测试就知道了 https://github.com/eggjs/egg-onerror/blob/master/test/fixtures/onerror-customize/config/config.default.js#L5

@atian25
Copy link
Member

atian25 commented Jul 2, 2018

这样 egg 会自动写到 error 日志里,但是我希望能拦截处理下,比如发个报警短信啥的

一般这种处理,不是在 egg 层做的,而是应该在日志采集分析层去做。因为一般集群多机情况下,日志采集回去后要做汇总分析才出告警的,这个有很多分析系统是可以直接用的。

@musicode
Copy link
Author

musicode commented Jul 2, 2018

多谢了,首先抱歉的是,我确实不太愿意去新建一个仓库,当然从你们的角度,这样做是最友好的。

我的配置其实很简单,在 config/plugin.js 加了一段

exports.onerror = {
    enable: true,
    package: 'egg-onerror',
}

然后在 config.default.js 加了一段:

config.onerror = {
    all(err, ctx) {
        console.log(err)
    }
}

然后抛一个错误:

throw new Error()

发现并没有打印。

现在我改了一种方式,用 app.on('error', function) 可以正常拦截。

最后,我现在实现比较简单,用了一个第三方的推送服务,只需要拦截错误发个 http 请求即可,所以我暂时先这么处理了,如有不妥,还烦请告知,谢谢了

@atian25
Copy link
Member

atian25 commented Jul 2, 2018

『如何向开源项目提交无法解答的问题』

这是节省双方的时间,最高效的交流方式。

像现在我们在 issue 上挤牙膏的交流了快 15 分钟,还是不知道问题出在哪里。

我猜测是应该是你应该还是配置错误了,最常见的那个错误估计:#2088

  1. 不需要配置 plugin.js
  2. config 的那个配置,你看看 [RFC] egg-bin check #2088 是不是你配错了,我又花了 2 分钟本地试了下是没问题的。

@musicode
Copy link
Author

musicode commented Jul 2, 2018

好的,下次我新建项目再提 issue,这次麻烦了

@haloworld007
Copy link

如果你在业务代码中写了try catch,那么你配置的onerror就不会执行了

@zwmmm
Copy link

zwmmm commented Jun 10, 2019

我也遇到这个问题了 并且检查了上面说的并没有错 @musicode

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

No branches or pull requests

5 participants