Skip to content
Snippets Groups Projects
Commit b28ac766 authored by Alok Joshi's avatar Alok Joshi
Browse files

cleaner refactor

parent 40acc237
No related branches found
No related tags found
1 merge request!737Handle large exception
......@@ -138,17 +138,10 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
private ResponseEntity<Object> getErrorResponse(AppException e) {
String exceptionMsg = e.getError().getMessage();
String exceptionMsgToLog = getExceptionMessageToLog(e.getError().getMessage());
if (canLogException(e)) {
logErrorOrWarning(exceptionMsg, e);
// log suppressed exception from Elastic's ResponseException if any
this.logSuppressedElasticException(e);
} else {
String loggingMsg = exceptionMsg.length() < configurationProperties.getMaxExceptionLogMessageLength() ? exceptionMsg : exceptionMsg.substring(0, configurationProperties.getMaxExceptionLogMessageLength());
logErrorOrWarning(loggingMsg, new AppException(e.getError().getCode(), e.getError().getReason(), loggingMsg));
}
logErrorOrWarning(exceptionMsgToLog, e);
logSuppressedElasticException(e);
// Support for non-standard HttpStatus Codes
HttpStatus httpStatus = HttpStatus.resolve(e.getError().getCode());
......@@ -167,19 +160,6 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
}
}
private boolean canLogException(AppException e) {
if (e.getMessage().length() > configurationProperties.getMaxExceptionLogMessageLength()) {
return false;
}
Exception cause = e.getOriginalException();
if (cause != null && cause.getSuppressed() != null) {
for (Throwable t : cause.getSuppressed()) {
if (t instanceof ResponseException && t.getMessage() != null && ((ResponseException) t).getResponse().getEntity().getContentLength() > configurationProperties.getMaxExceptionLogMessageLength()) return false;
}
}
return true;
}
private ObjectNode getValidationResponse(List<String> errors) {
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
......@@ -200,9 +180,18 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
Exception cause = e.getOriginalException();
if (cause != null && cause.getSuppressed() != null) {
for (Throwable t : cause.getSuppressed()) {
if (t instanceof ResponseException) this.jaxRsDpsLogger.error(t.getMessage(), (ResponseException) t);
if (t instanceof ResponseException) {
this.jaxRsDpsLogger.error(getExceptionMessageToLog(t.getMessage()), (ResponseException) t);
}
}
}
}
private String getExceptionMessageToLog(String originalExceptionMessage) {
if (originalExceptionMessage.length() > configurationProperties.getMaxExceptionLogMessageLength()) {
return originalExceptionMessage.substring(0, configurationProperties.getMaxExceptionLogMessageLength());
}
return originalExceptionMessage;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment