Skip to content

"Custom exception handling" example from NestJS Kafka docs doesn't work #14930

Closed
@kacperbudny-pc

Description

@kacperbudny-pc

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I tried implementing custom exception handling, but the example given in the docs doesn't work:

https://docs.nestjs.com/microservices/kafka#custom-exception-handling

The problem here is, when this exception filter runs, it fails with the following error:

ERROR [Consumer] Crash: KafkaJSNonRetriableError: Cannot read properties of undefined (reading 'isHeadersSent') 

I tried to look into the source code to find out why that happens, and it looks like BaseExceptionFilter assumes that we are in HTTP context, which we are not when working with Kafka. So I tried to re-throw the error instead, but this time it looks like the headers are always empty, so the retry count is always 0.

Minimum reproduction code

https://github.com/kacperbudny-pc/kafka-retries-example

Steps to reproduce

No response

Expected behavior

I can use the example from the docs to control the amount of retries

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

11.0.1

Packages versions

    "@nestjs/common": "^11.0.1",
    "@nestjs/core": "^11.0.1",
    "@nestjs/microservices": "^11.0.13",
    "@nestjs/platform-express": "^11.0.1",
    "kafkajs": "^2.2.4",
    "reflect-metadata": "^0.2.2",
    "rxjs": "^7.8.1"

Node.js version

22.12.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions