From bb7f3a53a6dd5782cf0e35070a99331bc6f8c097 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Thu, 29 Feb 2024 15:45:19 +0100 Subject: [PATCH] OZG-4949 Refactored Exception Controller into individual methods --- .../errorhandling/ExceptionController.java | 42 ++++++++++--------- .../ExceptionControllerITCase.java | 2 +- .../ExceptionControllerTest.java | 3 +- .../errorhandling/TestErrorController.java | 9 ++++ 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/ozgcloud/admin/common/errorhandling/ExceptionController.java b/src/main/java/de/ozgcloud/admin/common/errorhandling/ExceptionController.java index 4944af6b..db07ae30 100644 --- a/src/main/java/de/ozgcloud/admin/common/errorhandling/ExceptionController.java +++ b/src/main/java/de/ozgcloud/admin/common/errorhandling/ExceptionController.java @@ -46,25 +46,29 @@ import de.ozgcloud.common.errorhandling.TechnicalException; @RestControllerAdvice public class ExceptionController extends ResponseEntityExceptionHandler { - static final Map<Class<? extends Exception>, HttpStatus> STATUS_BY_EXCEPTION = Map.of( - RuntimeException.class, HttpStatus.INTERNAL_SERVER_ERROR, - AccessDeniedException.class, HttpStatus.FORBIDDEN, - ConstraintViolationException.class, HttpStatus.UNPROCESSABLE_ENTITY, - ResourceNotFoundException.class, HttpStatus.NOT_FOUND, - FunctionalException.class, HttpStatus.BAD_REQUEST, - TechnicalException.class, HttpStatus.INTERNAL_SERVER_ERROR); - - @ExceptionHandler({ - RuntimeException.class, - AccessDeniedException.class, - ResourceNotFoundException.class, - FunctionalException.class, - TechnicalException.class - }) - public ErrorResponse handleException(RuntimeException ex) { - var status = STATUS_BY_EXCEPTION.getOrDefault(ex.getClass(), HttpStatus.INTERNAL_SERVER_ERROR); - - return ErrorResponse.builder(ex, status, ex.getLocalizedMessage()).build(); + @ExceptionHandler(RuntimeException.class) + public ErrorResponse handleRuntimeException(RuntimeException ex) { + return ErrorResponse.builder(ex, HttpStatus.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage()).build(); + } + + @ExceptionHandler(AccessDeniedException.class) + public ErrorResponse handleAccessDeniedException(AccessDeniedException ex) { + return ErrorResponse.builder(ex, HttpStatus.FORBIDDEN, ex.getLocalizedMessage()).build(); + } + + @ExceptionHandler(ResourceNotFoundException.class) + public ErrorResponse handleResourceNotFoundException(ResourceNotFoundException ex) { + return ErrorResponse.builder(ex, HttpStatus.NOT_FOUND, ex.getLocalizedMessage()).build(); + } + + @ExceptionHandler(FunctionalException.class) + public ErrorResponse handleFunctionalException(FunctionalException ex) { + return ErrorResponse.builder(ex, HttpStatus.BAD_REQUEST, ex.getLocalizedMessage()).build(); + } + + @ExceptionHandler(TechnicalException.class) + public ErrorResponse handleTechnicalException(TechnicalException ex) { + return ErrorResponse.builder(ex, HttpStatus.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage()).build(); } @ExceptionHandler(ConstraintViolationException.class) diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerITCase.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerITCase.java index d364dafa..9ba9d27b 100644 --- a/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerITCase.java +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerITCase.java @@ -72,7 +72,7 @@ class ExceptionControllerITCase { } private static Stream<Arguments> exceptionAndExpectedStatus() { - return ExceptionController.STATUS_BY_EXCEPTION.entrySet().stream().map(kv -> Arguments.of(kv.getKey(), kv.getValue())); + return TestErrorController.STATUS_BY_EXCEPTION.entrySet().stream().map(kv -> Arguments.of(kv.getKey(), kv.getValue())); } } diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerTest.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerTest.java index f33a3715..41f95cf4 100644 --- a/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerTest.java +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/ExceptionControllerTest.java @@ -21,7 +21,6 @@ */ package de.ozgcloud.admin.common.errorhandling; -import static de.ozgcloud.admin.common.errorhandling.ExceptionController.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -111,7 +110,7 @@ class ExceptionControllerTest { } private static Stream<Arguments> exceptionAndExpectedStatus() { - return STATUS_BY_EXCEPTION.entrySet().stream().map(kv -> Arguments.of(kv.getKey(), kv.getValue())); + return TestErrorController.STATUS_BY_EXCEPTION.entrySet().stream().map(kv -> Arguments.of(kv.getKey(), kv.getValue())); } @SneakyThrows diff --git a/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java b/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java index c1044604..c49345ed 100644 --- a/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java +++ b/src/test/java/de/ozgcloud/admin/common/errorhandling/TestErrorController.java @@ -27,6 +27,7 @@ import java.util.Map; import jakarta.validation.ConstraintViolationException; import org.springframework.data.rest.webmvc.ResourceNotFoundException; +import org.springframework.http.HttpStatus; import org.springframework.security.access.AccessDeniedException; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -44,6 +45,14 @@ class TestErrorController { Exception produceException(); } + static final Map<Class<? extends Exception>, HttpStatus> STATUS_BY_EXCEPTION = Map.of( + RuntimeException.class, HttpStatus.INTERNAL_SERVER_ERROR, + AccessDeniedException.class, HttpStatus.FORBIDDEN, + ConstraintViolationException.class, HttpStatus.UNPROCESSABLE_ENTITY, + ResourceNotFoundException.class, HttpStatus.NOT_FOUND, + FunctionalException.class, HttpStatus.BAD_REQUEST, + TechnicalException.class, HttpStatus.INTERNAL_SERVER_ERROR); + static final String ERROR_MESSAGE = "error message"; static Map<Class<? extends Exception>, ExceptionProducer> EXCEPTION_PRODUCER = Map.of( -- GitLab