-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBarControllerAdvice.java
38 lines (30 loc) · 1.33 KB
/
BarControllerAdvice.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
package me.chanjar.boot.controlleradvice;
import me.chanjar.exception.AnotherException;
import me.chanjar.exception.SomeException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice(assignableTypes = BarController.class)
public class BarControllerAdvice {
@ExceptionHandler(SomeException.class)
String handleSomeException(HttpServletRequest request, Throwable ex) {
return "/controlleradvice/some-ex-error";
}
@ExceptionHandler(AnotherException.class)
@ResponseBody
ResponseEntity<?> handleAnotherException(HttpServletRequest request, Throwable ex) {
HttpStatus status = getStatus(request);
return new ResponseEntity<>(new AnotherExceptionErrorMessage(status.value(), ex.getMessage()), status);
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute(WebUtils.ERROR_STATUS_CODE_ATTRIBUTE);
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
}