1

I'm very new to Kafka/microservices, and I'm not understanding how error handling should work on these.

If I have no RpcException filter in place, the default RpcExceptionHandler seems to just catch the error and the entire microservice application just hangs.

Creating an RpcException filter allows me to catch exceptions that I explicitly throw, but it doesn't do anything for unhandled exceptions. Those still crash the app.

What am I missing here?

Controller:

import { Controller, Get } from '@nestjs/common';
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { AppService } from './app.service';


@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}


  @MessagePattern('get-accounts')
  getUser(data: any) {
     return this.appService.getHello();
  }
}

Logs

[Nest] 24760  - 09/09/2022, 12:09:25 PM   ERROR [RpcExceptionsHandler] Request failed with status code 404
[Nest] 24760  - 09/09/2022, 12:09:25 PM   ERROR [RpcExceptionsHandler] undefined
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • What is returning 404? Kafka is not an HTTP service – OneCricketeer Sep 10 '22 at 01:40
  • My microservice is making HTTP GET calls to a different API. That API will presumably throw all sorts of HTTP errors. Edit: I think the answer on this is to create a global exception filter and return a custom built message format indicating the error. NestJS with Kafka does not seem to tolerate unhandled exceptions at all. – DaleCooper123 Sep 12 '22 at 13:59
  • Okay, sure, in that case, I'm not sure how the logs you've shown are relevant to Kafka, then – OneCricketeer Sep 12 '22 at 14:00
  • That's the default RpcExceptionsHandler in the Kakfa microservice. It's catching the HTTP error (you can see the "request failed with status code 404"), but the entire microservice app hangs here. – DaleCooper123 Sep 12 '22 at 14:06
  • I'm only vaguely aware of what NestJS offers, but can you explain your app more? What is `AppService`, and what are you expecting it to do, in relation to Kafka? Seems you want to consume a Kafka record , and then call some remote RPC / HTTP method? If you want to debug Kafka, you can simply `console.log(data)`, otherwise, I don't see how calling `appService.getHello()` is related to Kafka since you're doing nothing with the consumed record. If `getHello()` fails, then yeah, you should add `try-catch` around it – OneCricketeer Sep 12 '22 at 17:08
  • It look like [this question](https://stackoverflow.com/questions/71781544/how-to-create-a-nestjs-global-exception-filter-for-microservices/76701081#76701081). I already answered – harian Jul 17 '23 at 01:50

0 Answers0