在 Nestjs 中,错误处理对我来说非常混乱。我有微服务 A,它向微服务 B 发出请求。在微服务 BI 中,使用第 3 方库(web3)创建一些请求,并且不要使用 try-catch,以防该库中出现错误,我有以下日志:
[Nest] 399 - 07/01/2021, 6:31:55 PM [RpcExceptionsHandler] Returned error: insufficient funds for gas * price + value +3819ms
eth-svc_1 | Error: Returned error: insufficient funds for gas * price + value
eth-svc_1 | at Object.ErrorResponse (/usr/src/app/node_modules/web3-core-helpers/lib/errors.js:28:19)
eth-svc_1 | at /usr/src/app/node_modules/web3-core-requestmanager/lib/index.js:303:36
...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我的微服务 A 收到类似的信息
{ status: 'error', message: 'Internal server error' }
Run Code Online (Sandbox Code Playgroud)
所以我丢失了错误消息。在微服务 B 中,我尝试使用不同的过滤器来处理此类错误,但没有成功。例子:
@Catch(Error)
export class ErrorFilter implements RpcExceptionFilter {
catch(exception: any, host: ArgumentsHost): Observable<any> {
return …Run Code Online (Sandbox Code Playgroud)