Skip to content
Snippets Groups Projects
Commit bb7f3a53 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4949 Refactored Exception Controller into individual methods

parent 65b94613
No related branches found
No related tags found
No related merge requests found
...@@ -46,25 +46,29 @@ import de.ozgcloud.common.errorhandling.TechnicalException; ...@@ -46,25 +46,29 @@ import de.ozgcloud.common.errorhandling.TechnicalException;
@RestControllerAdvice @RestControllerAdvice
public class ExceptionController extends ResponseEntityExceptionHandler { public class ExceptionController extends ResponseEntityExceptionHandler {
static final Map<Class<? extends Exception>, HttpStatus> STATUS_BY_EXCEPTION = Map.of( @ExceptionHandler(RuntimeException.class)
RuntimeException.class, HttpStatus.INTERNAL_SERVER_ERROR, public ErrorResponse handleRuntimeException(RuntimeException ex) {
AccessDeniedException.class, HttpStatus.FORBIDDEN, return ErrorResponse.builder(ex, HttpStatus.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage()).build();
ConstraintViolationException.class, HttpStatus.UNPROCESSABLE_ENTITY, }
ResourceNotFoundException.class, HttpStatus.NOT_FOUND,
FunctionalException.class, HttpStatus.BAD_REQUEST, @ExceptionHandler(AccessDeniedException.class)
TechnicalException.class, HttpStatus.INTERNAL_SERVER_ERROR); public ErrorResponse handleAccessDeniedException(AccessDeniedException ex) {
return ErrorResponse.builder(ex, HttpStatus.FORBIDDEN, ex.getLocalizedMessage()).build();
@ExceptionHandler({ }
RuntimeException.class,
AccessDeniedException.class, @ExceptionHandler(ResourceNotFoundException.class)
ResourceNotFoundException.class, public ErrorResponse handleResourceNotFoundException(ResourceNotFoundException ex) {
FunctionalException.class, return ErrorResponse.builder(ex, HttpStatus.NOT_FOUND, ex.getLocalizedMessage()).build();
TechnicalException.class }
})
public ErrorResponse handleException(RuntimeException ex) { @ExceptionHandler(FunctionalException.class)
var status = STATUS_BY_EXCEPTION.getOrDefault(ex.getClass(), HttpStatus.INTERNAL_SERVER_ERROR); public ErrorResponse handleFunctionalException(FunctionalException ex) {
return ErrorResponse.builder(ex, HttpStatus.BAD_REQUEST, ex.getLocalizedMessage()).build();
return ErrorResponse.builder(ex, status, 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) @ExceptionHandler(ConstraintViolationException.class)
......
...@@ -72,7 +72,7 @@ class ExceptionControllerITCase { ...@@ -72,7 +72,7 @@ class ExceptionControllerITCase {
} }
private static Stream<Arguments> exceptionAndExpectedStatus() { 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()));
} }
} }
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
*/ */
package de.ozgcloud.admin.common.errorhandling; 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.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
...@@ -111,7 +110,7 @@ class ExceptionControllerTest { ...@@ -111,7 +110,7 @@ class ExceptionControllerTest {
} }
private static Stream<Arguments> exceptionAndExpectedStatus() { 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 @SneakyThrows
......
...@@ -27,6 +27,7 @@ import java.util.Map; ...@@ -27,6 +27,7 @@ import java.util.Map;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -44,6 +45,14 @@ class TestErrorController { ...@@ -44,6 +45,14 @@ class TestErrorController {
Exception produceException(); 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 final String ERROR_MESSAGE = "error message";
static Map<Class<? extends Exception>, ExceptionProducer> EXCEPTION_PRODUCER = Map.of( static Map<Class<? extends Exception>, ExceptionProducer> EXCEPTION_PRODUCER = Map.of(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment