Skip to content

Commit

Permalink
Attach IO error to parse error when we can't contact Avro schema regi…
Browse files Browse the repository at this point in the history
…stry.

The change in apache#12080 lost the original exception context. This patch
adds it back.
  • Loading branch information
gianm committed Nov 21, 2022
1 parent fa3ab27 commit 7b09b0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ public GenericRecord parse(ByteBuffer bytes)
schema = parsedSchema instanceof AvroSchema ? ((AvroSchema) parsedSchema).rawSchema() : null;
}
catch (IOException | RestClientException ex) {
throw new ParseException(null, "Failed to get Avro schema: %s", id);
throw new ParseException(null, ex, "Failed to fetch Avro schema from registry: %s (%s)", id);
}
if (schema == null) {
throw new ParseException(null, "Failed to find Avro schema: %s", id);
throw new ParseException(null, "No Avro schema in registry: %s", id);
}
DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
try {
return reader.read(null, DecoderFactory.get().binaryDecoder(bytes.array(), offset, length, null));
}
catch (Exception e) {
throw new ParseException(null, e, "Fail to decode Avro message for schema: %s!", id);
throw new ParseException(null, e, "Failed to decode Avro message for schema: %s", id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.parsers.ParseException;
import org.apache.druid.utils.DynamicConfigProviderUtils;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -158,15 +160,20 @@ public void testParseWrongSchemaType() throws Exception
new SchemaRegistryBasedAvroBytesDecoder(registry).parse(bb);
}

@Test(expected = ParseException.class)
@Test
public void testParseWrongId() throws Exception
{
// Given
Mockito.when(registry.getSchemaById(ArgumentMatchers.anyInt())).thenThrow(new IOException("no pasaran"));
ByteBuffer bb = ByteBuffer.allocate(5).put((byte) 0).putInt(1234);
bb.rewind();
// When
new SchemaRegistryBasedAvroBytesDecoder(registry).parse(bb);
final ParseException e = Assert.assertThrows(
ParseException.class,
() -> new SchemaRegistryBasedAvroBytesDecoder(registry).parse(bb)
);
MatcherAssert.assertThat(e.getCause(), CoreMatchers.instanceOf(IOException.class));
MatcherAssert.assertThat(e.getCause().getMessage(), CoreMatchers.containsString("no pasaran"));
}

private byte[] getAvroDatum(Schema schema, GenericRecord someAvroDatum) throws IOException
Expand Down

0 comments on commit 7b09b0c

Please sign in to comment.