Skip to content
Snippets Groups Projects
Commit dfa2ca4b authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Fix exception handling

Exeption handling was broken by
56fe085e (GH-27).
parent 9d439cbc
Branches
Tags
No related merge requests found
......@@ -41,24 +41,10 @@ import java.util.HashMap;
import java.util.Map;
/**
* Translates security/authentication related exceptions into HTTP status codes.
* Translates exceptions into HTTP status codes.
*/
@ControllerAdvice
public class SecurityExceptionControllerAdvice {
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, String> defaultExceptionHandler(
final Exception e,
final HttpServletRequest req
) {
final Map<String, String> result = new HashMap<>();
result.put("code", "500");
result.put("status", "Internal server error");
result.put("message", e.getMessage());
return result;
}
public class ControllerExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
public void handleNotFoundException(final Exception e, final HttpServletRequest request) {
......
package de.thm.arsnova.controller;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
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 java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class DefaultExceptionHandler {
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Map<String, String> defaultExceptionHandler(
final Exception e,
final HttpServletRequest req
) throws Exception {
/* If the exception is annotated with @ResponseStatus rethrow it and let
* the framework handle it.
* See https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc. */
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;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment