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?
I send request onto an web api to my nestjs application
I have and event ObjectedCreatedEvent
is send it on the event bus after creation and write it into kafka (confluent.cloud).
Kafka responds with error 87 because the message validation against a json schema failed (that's ok so far)
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.
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.