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 4944af6b08a0aaed28a422f3e4dd1c2c6ec2e8cb..db07ae3081b0538c5d713c38f77171a5de797747 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 d364dafabda67317d9f3c22d5590133fce29ff65..9ba9d27bacdb1eccf4fe076f0bc556578ff43c40 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 f33a3715b32778a78b725e096ea0bf99e1fd4047..41f95cf47993da9c9a390d4e7d62702b2f0c79c7 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 c10446041aaba51ac761ac4e91ec01d6f9b7bb4b..c49345edcbebd221c8b19ab7fd3e08d11177f4ba 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(