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;
}
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;
}
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);
}
Aggregations