Skip to content

Commit

Permalink
AVRO-2114: Make missing value exceptions in nested structures easier …
Browse files Browse the repository at this point in the history
…to read.

This closes #263
  • Loading branch information
nielsbasjes committed Dec 15, 2017
1 parent ba11dda commit ff5099c
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Trunk (not yet released)
AVRO-2080: Fix Javadoc Warnings in Generated Records
(Bridger Howell via Niels Basjes)

AVRO-2114: Make missing value exceptions in nested structures easier to read.
(Niels Basjes)

BUG FIXES

AVRO-1741: Python3: Fix error when codec is not in the header.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.avro;

import org.apache.avro.Schema.Field;

import java.util.ArrayList;
import java.util.List;

/** Avro exception in case of missing fields. */
public class AvroMissingFieldException extends AvroRuntimeException {
private List<Field> chainOfFields = new ArrayList<>(8);
public AvroMissingFieldException(String message, Field field) {
super(message);
chainOfFields.add(field);
}

public void addParentField(Field field) {
chainOfFields.add(field);
}

@Override
public String toString() {
String result = "";
for (Field field: chainOfFields) {
result = " --> " + field.name() + result;
}
return "Path in schema:" + result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;

import org.apache.avro.AvroMissingFieldException;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.AvroTypeException;
import org.apache.avro.Conversion;
Expand Down Expand Up @@ -995,8 +996,8 @@ protected int compare(Object o1, Object o2, Schema s, boolean equals) {
public Object getDefaultValue(Field field) {
JsonNode json = field.defaultValue();
if (json == null)
throw new AvroRuntimeException("Field " + field
+ " not set and has no default value");
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
Expand Down
Loading

0 comments on commit ff5099c

Please sign in to comment.