summaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-19 02:35:57 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-19 02:42:10 +0100
commitaa929f6b2615ea66dc96b3daf8c505fa3c2d7388 (patch)
tree864e6b313fd5830abb4829f44ae62429fc33326a /src/main/java/org
parentAllow cross-origin for frontend server proxy (diff)
downloadseven-wonders-aa929f6b2615ea66dc96b3daf8c505fa3c2d7388.tar.gz
seven-wonders-aa929f6b2615ea66dc96b3daf8c505fa3c2d7388.tar.bz2
seven-wonders-aa929f6b2615ea66dc96b3daf8c505fa3c2d7388.zip
Improve error output
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java12
-rw-r--r--src/main/java/org/luxons/sevenwonders/errors/ErrorFactory.java52
-rw-r--r--src/main/java/org/luxons/sevenwonders/errors/ErrorType.java7
-rw-r--r--src/main/java/org/luxons/sevenwonders/errors/UIError.java42
-rw-r--r--src/main/java/org/luxons/sevenwonders/errors/UniqueIdAlreadyUsedException.java (renamed from src/main/java/org/luxons/sevenwonders/controllers/UniqueIdAlreadyUsedException.java)2
-rw-r--r--src/main/java/org/luxons/sevenwonders/errors/UserInputException.java14
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Lobby.java2
7 files changed, 126 insertions, 5 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
index 7567c330..e15935d6 100644
--- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
+++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java
@@ -7,6 +7,8 @@ import java.util.Map;
import org.luxons.sevenwonders.actions.JoinOrCreateGameAction;
import org.luxons.sevenwonders.actions.StartGameAction;
+import org.luxons.sevenwonders.errors.ErrorFactory;
+import org.luxons.sevenwonders.errors.UIError;
import org.luxons.sevenwonders.errors.UniqueIdAlreadyUsedException;
import org.luxons.sevenwonders.game.Game;
import org.luxons.sevenwonders.game.Lobby;
@@ -36,6 +38,8 @@ public class LobbyController {
private final SimpMessagingTemplate template;
+ private final ErrorFactory errorFactory;
+
private long lastGameId = 0;
private Map<String, Lobby> lobbies = new HashMap<>();
@@ -43,16 +47,18 @@ public class LobbyController {
private Map<String, Game> games = new HashMap<>();
@Autowired
- public LobbyController(GameDefinitionLoader gameDefinitionLoader, SimpMessagingTemplate template) {
+ public LobbyController(GameDefinitionLoader gameDefinitionLoader, SimpMessagingTemplate template,
+ ErrorFactory errorFactory) {
this.gameDefinitionLoader = gameDefinitionLoader;
this.template = template;
+ this.errorFactory = errorFactory;
}
@MessageExceptionHandler
@SendToUser("/queue/errors")
- public String handleException(Throwable exception) {
+ public UIError handleException(Throwable exception) {
logger.error("An error occured during message handling", exception);
- return exception.getClass().getSimpleName() + ": " + exception.getMessage();
+ return errorFactory.createError(exception);
}
@MessageMapping("/create-game")
diff --git a/src/main/java/org/luxons/sevenwonders/errors/ErrorFactory.java b/src/main/java/org/luxons/sevenwonders/errors/ErrorFactory.java
new file mode 100644
index 00000000..2d3dcd0b
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/errors/ErrorFactory.java
@@ -0,0 +1,52 @@
+package org.luxons.sevenwonders.errors;
+
+import java.util.List;
+import java.util.Locale;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.MessageSource;
+import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.ObjectError;
+
+@Component
+public class ErrorFactory {
+
+ private static final String ERROR_CODE_VALIDATION = "VALIDATION_ERROR";
+
+ private static final String ERROR_MSG_VALIDATION = "Input invalid";
+
+ private final MessageSource messageSource;
+
+ @Autowired
+ public ErrorFactory(MessageSource messageSource) {
+ this.messageSource = messageSource;
+ }
+
+ public UIError createError(Throwable exception) {
+ if (exception instanceof UserInputException) {
+ return createUserError((UserInputException)exception);
+ } else if (exception instanceof MethodArgumentNotValidException) {
+ return createValidationError((MethodArgumentNotValidException)exception);
+ } else {
+ return createInternalError(exception);
+ }
+ }
+
+ private UIError createUserError(UserInputException exception) {
+ String messageKey = exception.getMessageResourceKey();
+ String message = messageSource.getMessage(messageKey, null, messageKey, Locale.US);
+ return new UIError(messageKey, message, ErrorType.USER);
+ }
+
+ private UIError createInternalError(Throwable exception) {
+ return new UIError(exception.getClass().getSimpleName(), exception.getMessage(), ErrorType.INTERNAL);
+ }
+
+ private UIError createValidationError(MethodArgumentNotValidException exception) {
+ List<ObjectError> errors = exception.getBindingResult().getAllErrors();
+ UIError uiError = new UIError(ERROR_CODE_VALIDATION, ERROR_MSG_VALIDATION, ErrorType.VALIDATION);
+ uiError.setValidationErrors(errors);
+ return uiError;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java b/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java
new file mode 100644
index 00000000..71df185f
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/errors/ErrorType.java
@@ -0,0 +1,7 @@
+package org.luxons.sevenwonders.errors;
+
+public enum ErrorType {
+ USER,
+ VALIDATION,
+ INTERNAL
+}
diff --git a/src/main/java/org/luxons/sevenwonders/errors/UIError.java b/src/main/java/org/luxons/sevenwonders/errors/UIError.java
new file mode 100644
index 00000000..67802f22
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/errors/UIError.java
@@ -0,0 +1,42 @@
+package org.luxons.sevenwonders.errors;
+
+import java.util.List;
+
+import org.springframework.validation.ObjectError;
+
+public class UIError {
+
+ private final String code;
+
+ private final String message;
+
+ private final ErrorType type;
+
+ private List<ObjectError> validationErrors;
+
+ public UIError(String code, String message, ErrorType type) {
+ this.code = code;
+ this.message = message;
+ this.type = type;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public ErrorType getType() {
+ return type;
+ }
+
+ public List<ObjectError> getValidationErrors() {
+ return validationErrors;
+ }
+
+ public void setValidationErrors(List<ObjectError> validationErrors) {
+ this.validationErrors = validationErrors;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/controllers/UniqueIdAlreadyUsedException.java b/src/main/java/org/luxons/sevenwonders/errors/UniqueIdAlreadyUsedException.java
index 56c22655..b1b00453 100644
--- a/src/main/java/org/luxons/sevenwonders/controllers/UniqueIdAlreadyUsedException.java
+++ b/src/main/java/org/luxons/sevenwonders/errors/UniqueIdAlreadyUsedException.java
@@ -1,4 +1,4 @@
-package org.luxons.sevenwonders.controllers;
+package org.luxons.sevenwonders.errors;
public class UniqueIdAlreadyUsedException extends RuntimeException {
diff --git a/src/main/java/org/luxons/sevenwonders/errors/UserInputException.java b/src/main/java/org/luxons/sevenwonders/errors/UserInputException.java
new file mode 100644
index 00000000..11f7f8f2
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/errors/UserInputException.java
@@ -0,0 +1,14 @@
+package org.luxons.sevenwonders.errors;
+
+public class UserInputException extends RuntimeException {
+
+ private final String messageResourceKey;
+
+ public UserInputException(String messageResourceKey) {
+ this.messageResourceKey = messageResourceKey;
+ }
+
+ public String getMessageResourceKey() {
+ return messageResourceKey;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/Lobby.java b/src/main/java/org/luxons/sevenwonders/game/Lobby.java
index 7fdffef5..16d4e8a3 100644
--- a/src/main/java/org/luxons/sevenwonders/game/Lobby.java
+++ b/src/main/java/org/luxons/sevenwonders/game/Lobby.java
@@ -3,7 +3,7 @@ package org.luxons.sevenwonders.game;
import java.util.ArrayList;
import java.util.List;
-import org.luxons.sevenwonders.controllers.UniqueIdAlreadyUsedException;
+import org.luxons.sevenwonders.errors.UniqueIdAlreadyUsedException;
import org.luxons.sevenwonders.game.data.GameDefinition;
public class Lobby {
bgstack15