Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Submit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
EIP
Labrador
Submit
Merge requests
!110
Add error pages for different errors
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Add error pages for different errors
error-pages
into
development
Overview
0
Commits
1
Pipelines
0
Changes
8
Merged
Add error pages for different errors
Ruben Backx
requested to merge
error-pages
into
development
Aug 16, 2021
Overview
0
Commits
1
Pipelines
0
Changes
8
Closes
#93 (closed)
Edited
Aug 16, 2021
by
Ruben Backx
0
0
Merge request reports
Compare
development
version 1
9b6e67b7
Aug 16, 2021
development (base)
and
latest version
latest version
a78c79e4
1 commit,
Aug 16, 2021
version 1
9b6e67b7
1 commit,
Aug 16, 2021
8 files
+
256
−
17
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
8
src/main/java/nl/tudelft/submit/controller/ErrorController.java
+
95
−
3
View file @ a78c79e4
Edit in single-file editor
Open in Web IDE
Show full file
@@ -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