diff --git a/src/main/java/de/thm/arsnova/controller/AbstractControllerExceptionHandler.java b/src/main/java/de/thm/arsnova/controller/AbstractControllerExceptionHandler.java new file mode 100644 index 0000000000000000000000000000000000000000..c0baf0fa239c8a77d91bbd3a8b6f95c2a3775d71 --- /dev/null +++ b/src/main/java/de/thm/arsnova/controller/AbstractControllerExceptionHandler.java @@ -0,0 +1,14 @@ +package de.thm.arsnova.controller; + +import java.util.HashMap; +import java.util.Map; + +public class AbstractControllerExceptionHandler { + protected Map<String, Object> handleException(Throwable e) { + final Map<String, Object> result = new HashMap<>(); + result.put("errorType", e.getClass().getSimpleName()); + result.put("errorMessage", e.getMessage()); + + return result; + } +} diff --git a/src/main/java/de/thm/arsnova/controller/ControllerExceptionHandler.java b/src/main/java/de/thm/arsnova/controller/ControllerExceptionHandler.java index 6a8d22a40e7da66ef47a1a901ccc5813c55280dc..266a19ef273ef15d4528fed3d190565aea3f9253 100644 --- a/src/main/java/de/thm/arsnova/controller/ControllerExceptionHandler.java +++ b/src/main/java/de/thm/arsnova/controller/ControllerExceptionHandler.java @@ -33,87 +33,97 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.HashMap; import java.util.Map; /** * Translates exceptions into HTTP status codes. */ @ControllerAdvice -public class ControllerExceptionHandler { - @ResponseStatus(HttpStatus.NOT_FOUND) +public class ControllerExceptionHandler extends AbstractControllerExceptionHandler { @ExceptionHandler(NotFoundException.class) - public void handleNotFoundException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.NOT_FOUND) + public Map<String, Object> handleNotFoundException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.UNAUTHORIZED) @ExceptionHandler(UnauthorizedException.class) - public void handleUnauthorizedException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.UNAUTHORIZED) + public Map<String, Object> handleUnauthorizedException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.UNAUTHORIZED) @ExceptionHandler(AuthenticationCredentialsNotFoundException.class) - public void handleAuthenticationCredentialsNotFoundException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseStatus(HttpStatus.UNAUTHORIZED) + @ResponseBody + public Map<String, Object> handleAuthenticationCredentialsNotFoundException(final Exception e, final HttpServletRequest request) { + return handleException(e); } @ExceptionHandler(AccessDeniedException.class) - public void handleAccessDeniedException( + @ResponseBody + public Map<String, Object> handleAccessDeniedException( final Exception e, final HttpServletRequest request, final HttpServletResponse response ) { final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if ( - authentication == null + if (authentication == null || authentication.getPrincipal() == null - || authentication instanceof AnonymousAuthenticationToken - ) { + || authentication instanceof AnonymousAuthenticationToken) { response.setStatus(HttpStatus.UNAUTHORIZED.value()); - return; + } else { + response.setStatus(HttpStatus.FORBIDDEN.value()); } - response.setStatus(HttpStatus.FORBIDDEN.value()); + + return handleException(e); } - @ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler(ForbiddenException.class) - public void handleForbiddenException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.FORBIDDEN) + public Map<String, Object> handleForbiddenException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.NO_CONTENT) @ExceptionHandler(NoContentException.class) - public void handleNoContentException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.NO_CONTENT) + public Map<String, Object> handleNoContentException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(BadRequestException.class) - public void handleBadRequestException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.BAD_REQUEST) + public Map<String, Object> handleBadRequestException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.PRECONDITION_FAILED) @ExceptionHandler(PreconditionFailedException.class) - public void handlePreconditionFailedException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.PRECONDITION_FAILED) + public Map<String, Object> handlePreconditionFailedException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) @ExceptionHandler(NotImplementedException.class) - public void handleNotImplementedException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.NOT_IMPLEMENTED) + public Map<String, Object> handleNotImplementedException(final Exception e, final HttpServletRequest request) { + return handleException(e); } - @ResponseStatus(HttpStatus.PAYLOAD_TOO_LARGE) @ExceptionHandler(PayloadTooLargeException.class) - public void handlePayloadTooLargeException(final Exception e, final HttpServletRequest request) { - /* No implementation - handled solely by annotations */ + @ResponseBody + @ResponseStatus(HttpStatus.PAYLOAD_TOO_LARGE) + public Map<String, Object> handlePayloadTooLargeException(final Exception e, final HttpServletRequest request) { + return handleException(e); } } diff --git a/src/main/java/de/thm/arsnova/controller/DefaultExceptionHandler.java b/src/main/java/de/thm/arsnova/controller/DefaultControllerExceptionHandler.java similarity index 75% rename from src/main/java/de/thm/arsnova/controller/DefaultExceptionHandler.java rename to src/main/java/de/thm/arsnova/controller/DefaultControllerExceptionHandler.java index ba76e18e9d4afd5a23140d0225fa37dc2b752d26..fe8546c826bfe65e6c36cfc1a2b80d3f8d1eb25e 100644 --- a/src/main/java/de/thm/arsnova/controller/DefaultExceptionHandler.java +++ b/src/main/java/de/thm/arsnova/controller/DefaultControllerExceptionHandler.java @@ -8,15 +8,14 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; -import java.util.HashMap; import java.util.Map; @ControllerAdvice -public class DefaultExceptionHandler { +public class DefaultControllerExceptionHandler extends AbstractControllerExceptionHandler { @ExceptionHandler @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public Map<String, String> defaultExceptionHandler( + public Map<String, Object> defaultExceptionHandler( final Exception e, final HttpServletRequest req ) throws Exception { @@ -26,11 +25,7 @@ public class DefaultExceptionHandler { if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } - final Map<String, String> result = new HashMap<>(); - result.put("code", "500"); - result.put("status", "Internal server error"); - result.put("message", e.getMessage()); - return result; + return handleException(e); } }