Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idiomatic exception handling #88

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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