Search in sources :

Example 1 with AvroMissingFieldException

use of org.apache.avro.AvroMissingFieldException in project avro by apache.

the class GenericData method getDefaultValue.

/**
 * Gets the default value of the given field, if any.
 *
 * @param field the field whose default value should be retrieved.
 * @return the default value associated with the given field, or null if none is
 *         specified in the schema.
 */
@SuppressWarnings({ "unchecked" })
public Object getDefaultValue(Field field) {
    JsonNode json = Accessor.defaultValue(field);
    if (json == null)
        throw new AvroMissingFieldException("Field " + field + " not set and has no default value", field);
    if (json.isNull() && (field.schema().getType() == Type.NULL || (field.schema().getType() == Type.UNION && field.schema().getTypes().get(0).getType() == Type.NULL))) {
        return null;
    }
    // Check the cache
    Object defaultValue = defaultValueCache.get(field);
    // value and then decoding it:
    if (defaultValue == null)
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
            Accessor.encode(encoder, field.schema(), json);
            encoder.flush();
            BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(baos.toByteArray(), null);
            defaultValue = createDatumReader(field.schema()).read(null, decoder);
            // this MAY result in two threads creating the same defaultValue
            // and calling put. The last thread will win. However,
            // that's not an issue.
            defaultValueCache.put(field, defaultValue);
        } catch (IOException e) {
            throw new AvroRuntimeException(e);
        }
    return defaultValue;
}
Also used : AvroMissingFieldException(org.apache.avro.AvroMissingFieldException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) JsonNode(com.fasterxml.jackson.databind.JsonNode) AvroRuntimeException(org.apache.avro.AvroRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

Example 2 with AvroMissingFieldException

use of org.apache.avro.AvroMissingFieldException in project avro by a0x8o.

the class GenericData method getDefaultValue.

/**
 * Gets the default value of the given field, if any.
 *
 * @param field the field whose default value should be retrieved.
 * @return the default value associated with the given field, or null if none is
 *         specified in the schema.
 */
@SuppressWarnings({ "unchecked" })
public Object getDefaultValue(Field field) {
    JsonNode json = Accessor.defaultValue(field);
    if (json == null)
        throw new AvroMissingFieldException("Field " + field + " not set and has no default value", field);
    if (json.isNull() && (field.schema().getType() == Type.NULL || (field.schema().getType() == Type.UNION && field.schema().getTypes().get(0).getType() == Type.NULL))) {
        return null;
    }
    // Check the cache
    Object defaultValue = defaultValueCache.get(field);
    // value and then decoding it:
    if (defaultValue == null)
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
            Accessor.encode(encoder, field.schema(), json);
            encoder.flush();
            BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(baos.toByteArray(), null);
            defaultValue = createDatumReader(field.schema()).read(null, decoder);
            // this MAY result in two threads creating the same defaultValue
            // and calling put. The last thread will win. However,
            // that's not an issue.
            defaultValueCache.put(field, defaultValue);
        } catch (IOException e) {
            throw new AvroRuntimeException(e);
        }
    return defaultValue;
}
Also used : AvroMissingFieldException(org.apache.avro.AvroMissingFieldException) BinaryEncoder(org.apache.avro.io.BinaryEncoder) JsonNode(com.fasterxml.jackson.databind.JsonNode) AvroRuntimeException(org.apache.avro.AvroRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BinaryDecoder(org.apache.avro.io.BinaryDecoder)

Example 3 with AvroMissingFieldException

use of org.apache.avro.AvroMissingFieldException in project quick by bakdata.

the class AvroExceptionHandler method handle.

@Override
public HttpResponse<ErrorMessage> handle(final HttpRequest request, final AvroRuntimeException exception) {
    final ErrorMessage errorMessage;
    final String detail;
    if (exception instanceof AvroTypeException || exception instanceof AvroMissingFieldException || exception instanceof UnresolvedUnionException) {
        detail = String.format("Could not parse input: %s", exception.getMessage());
    } else if (exception instanceof AvroConversionException) {
        detail = String.format("An Error occurred during conversion of the Avro: %s", exception.getMessage());
    } else {
        errorMessage = HttpStatusError.toError(HttpStatus.INTERNAL_SERVER_ERROR, request.getPath(), exception.getMessage());
        return HttpResponse.<ErrorMessage>status(HttpStatus.valueOf(errorMessage.getCode())).body(errorMessage);
    }
    errorMessage = HttpStatusError.toError(HttpStatus.BAD_REQUEST, request.getPath(), detail);
    return HttpResponse.<ErrorMessage>status(HttpStatus.valueOf(errorMessage.getCode())).body(errorMessage);
}
Also used : AvroMissingFieldException(org.apache.avro.AvroMissingFieldException) AvroConversionException(tech.allegro.schema.json2avro.converter.AvroConversionException) ErrorMessage(com.bakdata.quick.common.api.model.ErrorMessage) UnresolvedUnionException(org.apache.avro.UnresolvedUnionException) AvroTypeException(org.apache.avro.AvroTypeException)

Aggregations

AvroMissingFieldException (org.apache.avro.AvroMissingFieldException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 AvroRuntimeException (org.apache.avro.AvroRuntimeException)2 BinaryDecoder (org.apache.avro.io.BinaryDecoder)2 BinaryEncoder (org.apache.avro.io.BinaryEncoder)2 ErrorMessage (com.bakdata.quick.common.api.model.ErrorMessage)1 AvroTypeException (org.apache.avro.AvroTypeException)1 UnresolvedUnionException (org.apache.avro.UnresolvedUnionException)1 AvroConversionException (tech.allegro.schema.json2avro.converter.AvroConversionException)1