I recently encountered some unexpected behavior in the code responsible for sending requests in my microservice. My primary goal is to handle prolonged requests, so I use RxJS timeout and catchError. However, TimeoutError ignores catchError and appears in the console, caught by the default NestJS exception interceptor. I know that I can use a global Interceptor to handle TimeoutErrors, and I have been using it, but to generate more detailed logs, I need additional context about the error, such as the transport name and method name. Here is my code:
return await firstValueFrom<Resp<TResult>>(
this.client
.send<Resp<TResult>, TMessage>(pattern, message)
.pipe(
timeout(this.rabbitMqTimeout),
catchError((e) => {
console.log(e)
if (e instanceof TimeoutError) {
this.logs.log("service timeout error", {
service: this.serviceName,
method: pattern,
payload: JSON.stringify(message),
})
} else {
this.logs.log("send to service error", {
service: this.serviceName,
method: pattern,
payload: JSON.stringify(message),
details: e.message,
})
}
return throwError(
() => new InternalServerErrorException(),
)
}),
),
)
I see plenty of examples where timeout errors are handled properly by catchError, but not in my case. I am not familiar with RxJS at all, so I am really desperate for your ideas.