0

I have got a bit of a problem,

I am using Kafka with CQRS in NestJS.

My question is: What is the best way to handle errors in the write process?

  1. I send request onto an web api to my nestjs application

  2. I have and event ObjectedCreatedEvent is send it on the event bus after creation and write it into kafka (confluent.cloud).

  3. Kafka responds with error 87 because the message validation against a json schema failed (that's ok so far)

  4. How can i respond properly to the web api that an error occured?

Just have started another listener in the process? I would expect kafka (confluent cloud) would have to topic for such things at least.

I would just have an Event ObjectValdiationFailedEvent and put it onto the eventbus.

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65
Fabian Börner
  • 168
  • 1
  • 2
  • 9

1 Answers1

3

Confluent Cloud won't help you much here since this is a client problem. As you may know; Kafka receives whatever has been serialized by the producer and simply stores the data into the selected partition. In your case data is not event leaving the client which means that whatever is throwing this error 87 certainly is not the server-side Kafka but the client-side.

My advice would be setting up any exception handler your framework NestJS supports. I am not a Node.js developer (My background is Java and Go) but a quick look into the NestJS documentation shows that this framework allows you to register filters capable of handling exceptions. For instance:

import { Catch, RpcExceptionFilter, ArgumentsHost } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { RpcException } from '@nestjs/microservices';

@Catch(RpcException)
export class ExceptionFilter implements RpcExceptionFilter<RpcException> {
  catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
    return throwError(exception.getError());
  }
}

More information here.

So you might want to investigate which layer is throwing this error 87 so you can handle it accordingly.

Ricardo Ferreira
  • 1,236
  • 3
  • 6
  • error 87 commes from kafka its a schema validation for the message its part of new definitions https://kafka.apache.org/protocol.html INVALID_RECORD | 87 | This record has failed the validation on broker and hence be rejected. – Fabian Börner Jun 26 '20 at 09:59
  • That is interesting. This means then that the produced record is not even compatible with the Kafka API and therefore, this is not a schema validation issue. – Ricardo Ferreira Jun 26 '20 at 15:06
  • This is a very generic error but it happens to me because of avro schema validation. Its not properly handled by the client in nestjs or kafkajs. Anyway my question was more how to properly get response in cqrs and my fault was that i assumed it has to be a sychronous respone what is not in cqrs. So what i have to do is send an event on kafka myself and make the consumer listen to it and the frontend will have to receive it asynchronous or a microservice that makes asynchronous events synchronous by waiting for request response cycle on the kafka bus. – Fabian Börner Jun 26 '20 at 15:25