I am unable get an exception when the program fails to connect to the kafka cluster. The code outputs the exception in the console logs but I need it throw an exception. I am using this c# library: https://github.com/confluentinc/confluent-kafka-dotnet
ProducerConfig _configKafka = new ProducerConfig { BootstrapServers ="localhost:9092/" };
ProducerBuilder<string, string> _kafkaProducer = new ProducerBuilder<string, string>(_configKafka);
using (var kafkaProducer = _kafkaProducer.Build())
{
try
{
var dr = kafkaProducer.ProduceAsync("Kafka_Messages", new Message<string, string> { Key = null, Value = $"message {i++}" });
dr.Wait(TimeSpan.FromSeconds(10));
if(dr.Exception!=null)
{
Console.WriteLine($"Delivery failed:");
}
var status = dr.Status;
//Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
}
catch (ProduceException<Null, string> e)
{
Console.WriteLine($"Delivery failed: {e.Error.Reason}");
}
}
Below is the error printed by confluent-kafka in console:
%3|1565248275.024|FAIL|rdkafka#producer-1| [thrd:localhst:9092/bootstrap]: localhst:9092/bootstrap: Failed to resolve 'localhst:9092': No such host is known. (after 2269ms in state CONNECT)
%3|1565248275.024|ERROR|rdkafka#producer-1| [thrd:localhst:9092/bootstrap]: localhst:9092/bootstrap: Failed to resolve 'localhst:9092': No such host is known. (after 2269ms in state CONNECT)
%3|1565248275.025|ERROR|rdkafka#producer-1| [thrd:localhst:9092/bootstrap]: 1/1 brokers are down
To get the actual exception within your application you need to add .SetErrorHandler()
:
ProducerBuilder<string, string> _kafkaProducer = new ProducerBuilder<string, string>(_configKafka);
using (var kafkaProducer = _kafkaProducer.SetErrorHandler((producer, error) =>
{
//You can handle error right here
}).Build())
error.Reason contains the error message