From deddd5ef5e8593009cd0e7dce699a459d497a727 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Mon, 16 Jan 2017 23:13:04 +0100 Subject: Separate client and server errors in the ExceptionHandler --- .../org/luxons/sevenwonders/errors/ErrorType.java | 4 +--- .../sevenwonders/errors/ExceptionHandler.java | 24 +++++++++++----------- 2 files changed, 13 insertions(+), 15 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java b/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java index a4a0f631..1cd18d09 100644 --- a/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java +++ b/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java @@ -1,7 +1,5 @@ package org.luxons.sevenwonders.errors; enum ErrorType { - USER, - VALIDATION, - INTERNAL + USER_INPUT, VALIDATION, CLIENT, SERVER } diff --git a/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java b/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java index 26a84ab2..628da4f8 100644 --- a/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java +++ b/src/main/java/org/luxons/sevenwonders/errors/ExceptionHandler.java @@ -35,6 +35,15 @@ public class ExceptionHandler { this.messageSource = messageSource; } + @MessageExceptionHandler + @SendToUser("/queue/errors") + public UIError handleUserInputError(UserInputException exception) { + logger.error("Incorrect user input: " + exception.getMessage()); + String messageKey = exception.getMessageResourceKey(); + String message = messageSource.getMessage(messageKey, exception.getParams(), messageKey, Locale.US); + return new UIError(messageKey, message, ErrorType.USER_INPUT); + } + @MessageExceptionHandler @SendToUser("/queue/errors") public UIError handleValidationError(MethodArgumentNotValidException exception) { @@ -43,7 +52,7 @@ public class ExceptionHandler { BindingResult result = exception.getBindingResult(); if (result != null) { - List errors = exception.getBindingResult().getAllErrors(); + List errors = result.getAllErrors(); uiError.addDetails(errors); } return uiError; @@ -56,26 +65,17 @@ public class ExceptionHandler { return new UIError(ERROR_CODE_CONVERSION, ERROR_MSG_CONVERSION, ErrorType.VALIDATION); } - @MessageExceptionHandler - @SendToUser("/queue/errors") - 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); - return new UIError(messageKey, message, ErrorType.USER); - } - @MessageExceptionHandler @SendToUser("/queue/errors") public UIError handleApiError(ApiMisuseException exception) { logger.error("Invalid API input", exception); - return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL); + return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.CLIENT); } @MessageExceptionHandler @SendToUser("/queue/errors") public UIError handleUnexpectedInternalError(Throwable exception) { logger.error("Uncaught exception thrown during message handling", exception); - return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL); + return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.SERVER); } } -- cgit