Throw an exception instead of using Optional<Resposne>

This commit is contained in:
Chris Eager 2022-02-15 17:46:27 -08:00 committed by Chris Eager
parent 73fa3c3fe4
commit ba5e5a780f
1 changed files with 3 additions and 9 deletions

View File

@ -202,12 +202,7 @@ public class MessageController {
contentLength += message.getBody().length(); contentLength += message.getBody().length();
} }
Optional<Response> maybeResponse = validateContentLength(contentLength, userAgent); validateContentLength(contentLength, userAgent);
if (maybeResponse.isPresent()) {
return maybeResponse.get();
}
validateEnvelopeType(message.getType(), userAgent); validateEnvelopeType(message.getType(), userAgent);
} }
@ -778,17 +773,16 @@ public class MessageController {
} }
} }
private Optional<Response> validateContentLength(final int contentLength, final String userAgent) { private void validateContentLength(final int contentLength, final String userAgent) {
Metrics.summary(CONTENT_SIZE_DISTRIBUTION_NAME, Tags.of(UserAgentTagUtil.getPlatformTag(userAgent))) Metrics.summary(CONTENT_SIZE_DISTRIBUTION_NAME, Tags.of(UserAgentTagUtil.getPlatformTag(userAgent)))
.record(contentLength); .record(contentLength);
if (contentLength > MAX_MESSAGE_SIZE) { if (contentLength > MAX_MESSAGE_SIZE) {
Metrics.counter(REJECT_OVERSIZE_MESSAGE_COUNTER, Tags.of(UserAgentTagUtil.getPlatformTag(userAgent))) Metrics.counter(REJECT_OVERSIZE_MESSAGE_COUNTER, Tags.of(UserAgentTagUtil.getPlatformTag(userAgent)))
.increment(); .increment();
return Optional.of(Response.status(Status.REQUEST_ENTITY_TOO_LARGE).build()); throw new WebApplicationException(Status.REQUEST_ENTITY_TOO_LARGE);
} }
return Optional.empty();
} }
private void validateEnvelopeType(final int type, final String userAgent) { private void validateEnvelopeType(final int type, final String userAgent) {