How to handle RpcExceptionsHandler

Viewed 745

Error handling is quiet messy for me in Nestjs. I have microservice A which makes requests to microservice B. In microservice B I use 3rd party library(web3) to create some requests, and don't use try-catch, in case if there is an error in that library I have the following log:

[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
...

In this case my microservice A receives something like

{ status: 'error', message: 'Internal server error' }

So I loose the message of error. In microservice B, I was trying to use different filters to handle this kind of error, but no luck. Example:

@Catch(Error)
export class ErrorFilter implements RpcExceptionFilter {
    catch(exception: any, host: ArgumentsHost): Observable<any> {
        return throwError({service: SERVICE_NAME, message: exception.message, statusCode: HttpStatus.INTERNAL_SERVER_ERROR});
    }
}

So, how to handle such unexpected errors and pass it properly to another microservices?

1 Answers

you said you dont wanna use try and catch right??. the 3rd party api might not handle that. use try and catch then return or throw the error that was catched

Related