package cz.moneta.test.harness.connectors.messaging.kafkautils;

import cz.moneta.test.harness.connectors.messaging.KafkaConnector;
import io.confluent.kafka.serializers.KafkaAvroDeserializer;
import org.apache.avro.Schema;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.header.Headers;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CustomKafkaAvroDeserializer extends KafkaAvroDeserializer {

    private static final Logger LOG = LogManager.getLogger(KafkaConnector.class);

    @Override
    public Object deserialize(String topic, byte[] bytes) {
        try {
            return super.deserialize(topic, bytes);
        } catch (SerializationException e) {
            LOG.error("Message deserialization by avro schema failed", e);
            return null;
        }
    }

    @Override
    public Object deserialize(String topic, Headers headers, byte[] bytes) {
        try {
            return super.deserialize(topic, headers, bytes);
        } catch (SerializationException e) {
            LOG.error("Message deserialization by avro schema failed", e);
            return null;
        }
    }

    @Override
    public Object deserialize(String topic, byte[] bytes, Schema readerSchema) {
        try {
            return super.deserialize(topic, bytes, readerSchema);
        } catch (SerializationException e) {
            LOG.error("Message deserialization by avro schema failed", e);
            return null;
        }
    }

    @Override
    public Object deserialize(String topic, Headers headers, byte[] bytes, Schema readerSchema) {
        try {
            return super.deserialize(topic, headers, bytes, readerSchema);
        } catch (SerializationException e) {
            LOG.error("Message deserialization by avro schema failed", e);
            return null;
        }
    }
}
