diff options
Diffstat (limited to 'juick-spring-www/src/main/java/com/juick')
-rw-r--r-- | juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java | 49 | ||||
-rw-r--r-- | juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java | 23 |
2 files changed, 61 insertions, 11 deletions
diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java new file mode 100644 index 00000000..a3e697b4 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java @@ -0,0 +1,49 @@ +package com.juick.www.controllers; + +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpForbiddenException; +import com.juick.server.util.HttpNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Created by aalexeev on 12/12/16. + */ +@ControllerAdvice +public class ErrorController { + private static Logger logger = LoggerFactory.getLogger(ErrorController.class); + + @ExceptionHandler(HttpBadRequestException.class) + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + public String badRequest() { + return "layout/error"; + } + + @ExceptionHandler(HttpForbiddenException.class) + @ResponseStatus(value = HttpStatus.FORBIDDEN) + public String forbidden() { + return "layout/error"; + } + + @ExceptionHandler(HttpNotFoundException.class) + @ResponseStatus(value = HttpStatus.NOT_FOUND) + public String notFound() { + return "layout/error"; + } + + @ExceptionHandler(Throwable.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public String exception(final Throwable throwable, final Model model) { + logger.error("Exception during execution of SpringSecurity application", throwable); + + String errorMessage = (throwable != null ? throwable.getMessage() : "Unknown error"); + model.addAttribute("errorMessage", errorMessage); + + return "layout/error"; + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java index e95bd7cf..df204c5f 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java @@ -1,6 +1,7 @@ package com.juick.www.controllers; import com.juick.User; +import com.juick.server.util.HttpNotFoundException; import com.juick.service.MessagesService; import com.juick.service.UserService; import com.juick.util.WebUtils; @@ -58,7 +59,7 @@ public class ShowMessageController { model.addAttribute("messageId", anything); - return "postNotFound"; + throw new HttpNotFoundException(); } } return showUserMessages(anything, null, before, model); @@ -88,7 +89,7 @@ public class ShowMessageController { User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -101,13 +102,13 @@ public class ShowMessageController { // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -120,13 +121,13 @@ public class ShowMessageController { // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -139,13 +140,13 @@ public class ShowMessageController { // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -159,19 +160,19 @@ public class ShowMessageController { // Check validity of post number before quering from database if (WebUtils.isNotPostNumber(postNumber)) { model.addAttribute("messageId", postNumber); - return "postNotFound"; + throw new HttpNotFoundException(); } // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } |