-
Notifications
You must be signed in to change notification settings - Fork 209
/
GlobalExceptionHandlingControllerAdvice.java
101 lines (86 loc) · 3.39 KB
/
GlobalExceptionHandlingControllerAdvice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package demo2.web;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
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.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import demo.exceptions.SupportInfoException;
import demo1.web.ExceptionHandlingController;
/**
* Performs the same exception handling as {@link ExceptionHandlingController}
* but offers them globally. The exceptions below could be raised by any
* controller and they would be handled here, if not handled in the controller
* already.
*
* @author Paul Chapman
*/
@ControllerAdvice
public class GlobalExceptionHandlingControllerAdvice {
protected Logger logger;
public GlobalExceptionHandlingControllerAdvice() {
logger = LoggerFactory.getLogger(getClass());
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* . . . . . . . . . . . . . EXCEPTION HANDLERS . . . . . . . . . . . . . . */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Convert a predefined exception to an HTTP Status code
*/
@ResponseStatus(value = HttpStatus.CONFLICT, reason = "Data integrity violation")
// 409
@ExceptionHandler(DataIntegrityViolationException.class)
public void conflict() {
logger.error("Request raised a DataIntegrityViolationException");
// Nothing to do
}
/**
* Convert a predefined exception to an HTTP Status code and specify the
* name of a specific view that will be used to display the error.
*
* @return Exception view.
*/
@ExceptionHandler({ SQLException.class, DataAccessException.class })
public String databaseError(Exception exception) {
// Nothing to do. Return value 'databaseError' used as logical view name
// of an error page, passed to view-resolver(s) in usual way.
logger.error("Request raised " + exception.getClass().getSimpleName());
return "databaseError";
}
/**
* Demonstrates how to take total control - setup a model, add useful
* information and return the "support" view name. This method explicitly
* creates and returns
*
* @param req
* Current HTTP request.
* @param exception
* The exception thrown - always {@link SupportInfoException}.
* @return The model and view used by the DispatcherServlet to generate
* output.
* @throws Exception
*/
@ExceptionHandler(SupportInfoException.class)
public ModelAndView handleError(HttpServletRequest req, Exception exception)
throws Exception {
// Rethrow annotated exceptions or they will be processed here instead.
if (AnnotationUtils.findAnnotation(exception.getClass(),
ResponseStatus.class) != null)
throw exception;
logger.error("Request: " + req.getRequestURI() + " raised " + exception);
ModelAndView mav = new ModelAndView();
mav.addObject("exception", exception);
mav.addObject("url", req.getRequestURL());
mav.addObject("timestamp", new Date().toString());
mav.addObject("status", 500);
mav.setViewName("support");
return mav;
}
}