egg icon indicating copy to clipboard operation
egg copied to clipboard

请教一个关于onerror异常处理的问题

Open mexhfl opened this issue 3 years ago • 6 comments

我安装egg官方文档中的异常处理,配置好了onerror,文档上说:Controller、Service中的异常全部能通过onerror捕获到,但是使用时发现:Controller中的异常能捕获到,没有问题。但是Service中的异常捕获不到。请教下这个是什么问题?如何解决这个问题?

mexhfl avatar Feb 24 '22 06:02 mexhfl

github 上传个捕获不到异常的例子一起看下呢?

hyj1991 avatar Feb 24 '22 06:02 hyj1991

github 上传个捕获不到异常的例子一起看下呢?

我又进一步测试了一下,发现是service方法体中的异常可以捕获到,但是service中promise方法体内的异常捕获不到,这个有什么办法呢?我随后附上代码

mexhfl avatar Feb 24 '22 06:02 mexhfl

image

mexhfl avatar Feb 24 '22 06:02 mexhfl

promise中的异常报了这个错,ERROR 95796 nodejs.unhandledRejectionError: 这个异常捕获不到!!是否有什么办法把这个错捕获到?然后把错误信息记录到日志中,并给前端返回一些说明

mexhfl avatar Feb 24 '22 07:02 mexhfl

promise中的异常报了这个错,ERROR 95796 nodejs.unhandledRejectionError: 这个异常捕获不到!!是否有什么办法把这个错捕获到?然后把错误信息记录到日志中,并给前端返回一些说明

可以考虑使用 node 中 domain 模块,来解决由于try/catch无法捕捉异步回调里的异常

ddzyan avatar Mar 16 '22 10:03 ddzyan

promise中的异常报了这个错,ERROR 95796 nodejs.unhandledRejectionError: 这个异常捕获不到!!是否有什么办法把这个错捕获到?然后把错误信息记录到日志中,并给前端返回一些说明

Controller调用Service,Service 调用中返回了一个promise,然后整条链路就结束了。框架层在最外层给一次请求的try catch范围也就结束了 等到后面Promise中逻辑执行时,已经没有 try catch环境了,所以捕获不到。

smileEvday avatar Dec 08 '23 08:12 smileEvday