Skip to content

Commit

Permalink
[WOR-1798] Indicate which fields are missing when giving back a 400 o…
Browse files Browse the repository at this point in the history
…n MethodArgumentNotValidException (#1779)
  • Loading branch information
aherbst-broad authored Aug 6, 2024
1 parent b0b249c commit 3102651
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import bio.terra.common.exception.NotFoundException;
import bio.terra.workspace.generated.model.ApiErrorReport;
import jakarta.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
Expand All @@ -12,6 +13,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.retry.backoff.BackOffInterruptedException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand Down Expand Up @@ -44,7 +47,6 @@ public ResponseEntity<ApiErrorReport> errorReportHandler(ErrorReportException ex

// -- validation exceptions - we don't control the exception raised
@ExceptionHandler({
MethodArgumentNotValidException.class,
MethodArgumentTypeMismatchException.class,
HttpMessageNotReadableException.class,
HttpRequestMethodNotSupportedException.class,
Expand All @@ -67,6 +69,31 @@ public ResponseEntity<ApiErrorReport> validationExceptionHandler(Exception ex) {
return new ResponseEntity<>(errorReport, HttpStatus.BAD_REQUEST);
}

/** Give back the fields missing from the request that caused the exception. */
@ExceptionHandler({MethodArgumentNotValidException.class})
public ResponseEntity<ApiErrorReport> methodArgNotValidHandler(
MethodArgumentNotValidException ex) {
final List<String> errors = new ArrayList<>();
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
errors.add(error.getField() + ": " + error.getDefaultMessage());
}
for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
}

String validationErrorMessage =
"Request could not be parsed or was invalid: "
+ ex.getClass().getSimpleName()
+ ". Ensure that all types are correct and that enums have valid values.";
ApiErrorReport errorReport =
new ApiErrorReport()
.message(validationErrorMessage)
.statusCode(HttpStatus.BAD_REQUEST.value())
.causes(errors);

return new ResponseEntity<>(errorReport, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<ApiErrorReport> constraintViolationExceptionHandler(
ConstraintViolationException ex) {
Expand Down

0 comments on commit 3102651

Please sign in to comment.