Commit 3f9f789c authored by Matija Obreza's avatar Matija Obreza
Browse files

Handle AccessDenied exceptions

parent b5ef0308
......@@ -22,6 +22,7 @@ import org.genesys.catalog.exceptions.NotFoundElement;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
......@@ -48,13 +49,21 @@ public class ApiExceptionHandler {
// }
@ResponseStatus(code = HttpStatus.FORBIDDEN)
@ExceptionHandler(AuthenticationCredentialsNotFoundException.class)
@ExceptionHandler({ AuthenticationCredentialsNotFoundException.class })
@ResponseBody
public ApiError<Exception> handleMissingCredentials(final Exception e, final WebRequest request) {
LOG.warn("Authentication is required.", e);
return new ApiError<>(e);
}
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
@ExceptionHandler({ AccessDeniedException.class })
@ResponseBody
public ApiError<Exception> handleAccessDenied(final Exception e, final WebRequest request) {
LOG.warn("Authentication is required.", e);
return new ApiError<>(e);
}
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
......@@ -70,6 +79,14 @@ public class ApiExceptionHandler {
LOG.warn("Invalid payload provided.", e);
return new ApiError<>(e);
}
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundElement.class)
@ResponseBody
public ApiError<Exception> handleNotFound(final Exception e, final WebRequest request) {
LOG.warn("Element not found", e);
return new ApiError<>(e);
}
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Throwable.class)
......@@ -78,12 +95,4 @@ public class ApiExceptionHandler {
LOG.error("Wow! Such! Exception!", e);
return new ApiError<>(e);
}
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundElement.class)
@ResponseBody
public ApiError<Exception> handleNotFound(final Exception e, final WebRequest request) {
LOG.warn("Element not found", e);
return new ApiError<>(e);
}
}
Supports Markdown
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