Skip to content

Commit

Permalink
Idiomatic exception handling
Browse files Browse the repository at this point in the history
Reduced cyclomatic complexity; i.e. fewer branches and tests
  • Loading branch information
elharo authored and tdcmeehan committed Aug 20, 2024
1 parent b0d4fcd commit f039442
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions jaxrs/src/main/java/com/facebook/airlift/jaxrs/JsonMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,30 @@ public Object readFrom(Class<Object> type,
InputStream inputStream)
throws IOException
{
Object object;
try {
JsonParser jsonParser = objectMapper.getFactory().createParser(inputStream);

// Important: we are NOT to close the underlying stream after
// mapping, so we need to instruct parser:
jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);

object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
Object object = objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(genericType));
return object;
}
catch (Exception e) {
// We want to handle parsing exceptions differently than regular IOExceptions so just rethrow IOExceptions
if (e instanceof IOException && !(e instanceof JsonProcessingException) && !(e instanceof EOFException)) {
throw e;
}
catch (JsonProcessingException | EOFException e) {
log.debug(e, "Invalid json for Java type %s", type);

// Invalid json request. Throwing exception so the response code can be overridden using a mapper.
throw new JsonMapperParsingException(type, e);
}
catch (RuntimeException e) {
// log the exception at debug so it can be viewed during development
// Note: we are not logging at a higher level because this could cause a denial of service
log.debug(e, "Invalid json for Java type %s", type);

// Invalid json request. Throwing exception so the response code can be overridden using a mapper.
throw new JsonMapperParsingException(type, e);
}
return object;
}

@Override
Expand Down

0 comments on commit f039442

Please sign in to comment.