Skip to content
Snippets Groups Projects

Add error pages for different errors

8 files
+ 256
17
Compare changes
  • Side-by-side
  • Inline

Files

@@ -17,13 +17,27 @@
*/
package nl.tudelft.submit.controller;
import javax.servlet.http.HttpServletResponse;
import nl.tudelft.librador.exception.DTOValidationException;
import nl.tudelft.submit.exception.DisallowedUploadException;
import nl.tudelft.submit.exception.ScoreFormatException;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import io.sentry.Sentry;
@ControllerAdvice
public class ErrorController {
public class ErrorController extends ResponseEntityExceptionHandler {
/**
* Prints the exception stack trace and returns the error page with the default error message.
*
@@ -31,10 +45,88 @@ public class ErrorController {
* @return the link to the error page
*/
@ExceptionHandler(Exception.class)
public void exception(Exception e) throws Exception {
public String exception(Exception e) throws Exception {
e.printStackTrace();
Sentry.captureException(e);
throw (e);
return "error/error";
}
/**
* Returns the error page when a request is forbidden.
*
* @return The page to load
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(AccessDeniedException.class)
public String forbidden() {
return "error/forbidden";
}
/**
* Returns the error page when a resource is not found.
*
* @return The page to load
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ResourceNotFoundException.class)
public String notFound() {
return "error/not_found";
}
/**
* Returns the error page when a request is a bad request.
*
* @return The page to load
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ WebClientResponseException.BadRequest.class, DisallowedUploadException.class,
ScoreFormatException.class })
public String badRequest() {
return "error/bad_request";
}
/**
* Returns the error page when a request contains an invalid entity.
*
* @return The page to load
*/
@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
@ExceptionHandler(DTOValidationException.class)
public String unprocessableEntity() {
return "error/unprocessable_entity";
}
/**
* Handles a response status exception.
*
* @return The page to load
*/
@ExceptionHandler(ResponseStatusException.class)
public String responseStatusException(ResponseStatusException e, HttpServletResponse resp)
throws Exception {
resp.setStatus(e.getStatus().value());
switch (e.getStatus()) {
case FORBIDDEN:
return forbidden();
case NOT_FOUND:
return notFound();
case UNPROCESSABLE_ENTITY:
return unprocessableEntity();
default:
return exception(e);
}
}
/**
* Handles any leftover exceptions.
*
* @return The page to load
*/
@GetMapping("/error")
public String defaultErrorMappingHandler(Exception e) throws Exception {
return exception(e);
}
}
Loading