diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-14 15:51:44 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2017-01-14 15:53:23 +0100 |
commit | 80c46ea7900fc6b4fb8c33de6228344ff852967f (patch) | |
tree | 431ca82ef43219ef93db684e73c9219995175c27 /src/main | |
parent | Add test for CardBack initialization (coverage frenzy) (diff) | |
download | seven-wonders-80c46ea7900fc6b4fb8c33de6228344ff852967f.tar.gz seven-wonders-80c46ea7900fc6b4fb8c33de6228344ff852967f.tar.bz2 seven-wonders-80c46ea7900fc6b4fb8c33de6228344ff852967f.zip |
Simplify validation errors' output
Diffstat (limited to 'src/main')
4 files changed, 70 insertions, 16 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java b/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java index 71df185f..a4a0f631 100644 --- a/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java +++ b/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.errors; -public enum ErrorType { +enum ErrorType { USER, VALIDATION, INTERNAL diff --git a/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java b/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java index 9e26cbe3..26a84ab2 100644 --- a/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java +++ b/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java @@ -11,6 +11,7 @@ import org.springframework.messaging.converter.MessageConversionException; import org.springframework.messaging.handler.annotation.MessageExceptionHandler; import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException; import org.springframework.messaging.simp.annotation.SendToUser; +import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -19,9 +20,9 @@ public class ExceptionHandler { private static final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class); - private static final String ERROR_CODE_VALIDATION = "VALIDATION_ERROR"; + private static final String ERROR_CODE_VALIDATION = "INVALID_DATA"; - private static final String ERROR_CODE_CONVERSION = "MESSAGE_FORMAT_ERROR"; + private static final String ERROR_CODE_CONVERSION = "INVALID_MESSAGE_FORMAT"; private static final String ERROR_MSG_VALIDATION = "Invalid input data"; @@ -36,24 +37,28 @@ public class ExceptionHandler { @MessageExceptionHandler @SendToUser("/queue/errors") - private UIError handleValidationError(MethodArgumentNotValidException exception) { + public UIError handleValidationError(MethodArgumentNotValidException exception) { logger.error("Invalid input", exception); - List<ObjectError> errors = exception.getBindingResult().getAllErrors(); UIError uiError = new UIError(ERROR_CODE_VALIDATION, ERROR_MSG_VALIDATION, ErrorType.VALIDATION); - uiError.setValidationErrors(errors); + + BindingResult result = exception.getBindingResult(); + if (result != null) { + List<ObjectError> errors = exception.getBindingResult().getAllErrors(); + uiError.addDetails(errors); + } return uiError; } @MessageExceptionHandler @SendToUser("/queue/errors") - private UIError handleConversionError(MessageConversionException exception) { + public UIError handleConversionError(MessageConversionException exception) { logger.error("Error interpreting the message", exception); return new UIError(ERROR_CODE_CONVERSION, ERROR_MSG_CONVERSION, ErrorType.VALIDATION); } @MessageExceptionHandler @SendToUser("/queue/errors") - private UIError handleGenericUserError(UserInputException exception) { + public UIError handleGenericUserError(UserInputException exception) { logger.error("Incorrect user input: " + exception.getMessage()); String messageKey = exception.getMessageResourceKey(); String message = messageSource.getMessage(messageKey, exception.getParams(), messageKey, Locale.US); @@ -62,14 +67,14 @@ public class ExceptionHandler { @MessageExceptionHandler @SendToUser("/queue/errors") - private UIError handleApiError(ApiMisuseException exception) { + public UIError handleApiError(ApiMisuseException exception) { logger.error("Invalid API input", exception); return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL); } @MessageExceptionHandler @SendToUser("/queue/errors") - private UIError handleUnexpectedInternalError(Throwable exception) { + public UIError handleUnexpectedInternalError(Throwable exception) { logger.error("Uncaught exception thrown during message handling", exception); return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL); } diff --git a/src/main/java/org/luxons/sevenwonders/errors/UIError.java b/src/main/java/org/luxons/sevenwonders/errors/UIError.java index 67802f22..ee5fcbe0 100644 --- a/src/main/java/org/luxons/sevenwonders/errors/UIError.java +++ b/src/main/java/org/luxons/sevenwonders/errors/UIError.java @@ -1,7 +1,9 @@ package org.luxons.sevenwonders.errors; +import java.util.ArrayList; import java.util.List; +import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; public class UIError { @@ -12,9 +14,9 @@ public class UIError { private final ErrorType type; - private List<ObjectError> validationErrors; + private List<UIErrorDetail> details = new ArrayList<>(); - public UIError(String code, String message, ErrorType type) { + UIError(String code, String message, ErrorType type) { this.code = code; this.message = message; this.type = type; @@ -32,11 +34,21 @@ public class UIError { return type; } - public List<ObjectError> getValidationErrors() { - return validationErrors; + public List<UIErrorDetail> getDetails() { + return details; } - public void setValidationErrors(List<ObjectError> validationErrors) { - this.validationErrors = validationErrors; + void addDetails(List<ObjectError> objectErrors) { + for (ObjectError objectError : objectErrors) { + this.details.add(convertError(objectError)); + } + } + + private UIErrorDetail convertError(ObjectError objectError) { + if (objectError instanceof FieldError) { + return new UIErrorDetail((FieldError)objectError); + } else { + return new UIErrorDetail(objectError); + } } } diff --git a/src/main/java/org/luxons/sevenwonders/errors/UIErrorDetail.java b/src/main/java/org/luxons/sevenwonders/errors/UIErrorDetail.java new file mode 100644 index 00000000..dc4250bb --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/errors/UIErrorDetail.java @@ -0,0 +1,37 @@ +package org.luxons.sevenwonders.errors; + +import org.springframework.validation.FieldError; +import org.springframework.validation.ObjectError; + +class UIErrorDetail { + + private final Object rejectedValue; + + private final String path; + + private final String message; + + UIErrorDetail(FieldError error) { + rejectedValue = error.getRejectedValue(); + path = error.getObjectName() + '.' + error.getField(); + message = "Invalid value for field '" + error.getField() + "': " + error.getDefaultMessage(); + } + + UIErrorDetail(ObjectError error) { + rejectedValue = null; + path = error.getObjectName(); + message = "Invalid value for object '" + error.getObjectName() + "': " + error.getDefaultMessage(); + } + + public Object getRejectedValue() { + return rejectedValue; + } + + public String getPath() { + return path; + } + + public String getMessage() { + return message; + } +} |