forked from Realhedin/topjava02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionInfoHandler.java
More file actions
55 lines (48 loc) · 2.02 KB
/
ExceptionInfoHandler.java
File metadata and controls
55 lines (48 loc) · 2.02 KB
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
package ru.javawebinar.topjava.web;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import ru.javawebinar.topjava.LoggerWrapper;
import ru.javawebinar.topjava.util.exception.ErrorInfo;
import ru.javawebinar.topjava.util.exception.NotFoundException;
import ru.javawebinar.topjava.util.exception.ValidationException;
import javax.servlet.http.HttpServletRequest;
/**
* User: gkislin
* Date: 23.09.2014
*/
public class ExceptionInfoHandler {
protected final LoggerWrapper LOG = LoggerWrapper.get(getClass());
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
@ResponseBody
@Order(Ordered.HIGHEST_PRECEDENCE)
ErrorInfo handleError(HttpServletRequest req, NotFoundException e) {
return LOG.getErrorInfo(req.getRequestURL(), e);
}
@ResponseStatus(value = HttpStatus.CONFLICT) // 409
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseBody
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
ErrorInfo conflict(HttpServletRequest req, DataIntegrityViolationException e) {
return LOG.getErrorInfo(req.getRequestURL(), e);
}
@ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY) // 422
@ExceptionHandler(ValidationException.class)
@ResponseBody
@Order(Ordered.HIGHEST_PRECEDENCE + 2)
ErrorInfo validationError(HttpServletRequest req, ValidationException e) {
return LOG.getErrorInfo(req.getRequestURL(), e);
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
@ResponseBody
@Order(Ordered.LOWEST_PRECEDENCE)
ErrorInfo handleError(HttpServletRequest req, Exception e) {
return LOG.getErrorInfo(req.getRequestURL(), e);
}
}