diff --git a/src/main/java/edu/umd/dawn/common/exceptions/CustomExceptionHandler.java b/src/main/java/edu/umd/dawn/common/exceptions/CustomExceptionHandler.java index 843e7a4556c3455f101f1549151b8a8289797cf1..9bf4cf5c3eee65fff39422a526a70bd292c7a525 100644 --- a/src/main/java/edu/umd/dawn/common/exceptions/CustomExceptionHandler.java +++ b/src/main/java/edu/umd/dawn/common/exceptions/CustomExceptionHandler.java @@ -29,17 +29,21 @@ public class CustomExceptionHandler { return exceptionAsString; } - private ResponseEntity<Object> returnDawnException(DawnException exception) { + private void logException(DawnException exception) { if (exception.getBase() != null) { MDC.put("stackTrace", getStackTrace(exception.getBase())); MDC.put("exceptionMessage", exception.getBase().getMessage()); } else { MDC.put("stackTrace", getStackTrace(exception)); MDC.put("exceptionMessage", exception.getMessage()); - // MDC.put("exceptionMessage", exception.) } log.error(exception.getMessage()); MDC.remove("stackTrace"); + MDC.remove("exceptionMessage"); + } + + private ResponseEntity<Object> returnDawnException(DawnException exception) { + logException(exception); return ExceptionResponse.fromDawnException(exception, source).toResponse(); } @@ -51,15 +55,12 @@ public class CustomExceptionHandler { @ExceptionHandler(BindException.class) protected ResponseEntity<Object> handleBindException(BindException ex, WebRequest request) { + DawnException wrapped = new DawnException(BaseExceptions.BAD_REQUEST, ex); + FieldError err = ex.getFieldError(); + wrapped.setDescription("Value " + err.getRejectedValue() + " is invalid for field " + err.getField()); - ExceptionResponse response = ExceptionResponse.builder() - .source(source) - .description("Value " + err.getRejectedValue() + " is invalid for field " + err.getField()) - .errorCode("BAD_REQUEST") - .build() - .buildDetails(); - return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + return returnDawnException(wrapped); } @ExceptionHandler(ArrayIndexOutOfBoundsException.class) diff --git a/src/main/java/edu/umd/dawn/common/exceptions/DawnException.java b/src/main/java/edu/umd/dawn/common/exceptions/DawnException.java index 3005b86c430710ee00ed7b14d5a40300da525394..11b4e750d8b95b505f03926b097a6c67bd0d817c 100644 --- a/src/main/java/edu/umd/dawn/common/exceptions/DawnException.java +++ b/src/main/java/edu/umd/dawn/common/exceptions/DawnException.java @@ -34,4 +34,8 @@ public class DawnException extends RuntimeException { } return super.getStackTrace(); } + + public void setDescription(String description) { + this.description = description; + } }