1
|
|
|
package com.osomapps.pt; |
2
|
|
|
|
3
|
|
|
import java.util.Map; |
4
|
|
|
import javax.servlet.http.HttpServletResponse; |
5
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
6
|
|
|
import org.springframework.beans.factory.annotation.Value; |
7
|
|
|
import org.springframework.boot.web.error.ErrorAttributeOptions; |
8
|
|
|
import org.springframework.boot.web.servlet.error.ErrorAttributes; |
9
|
|
|
import org.springframework.boot.web.servlet.error.ErrorController; |
10
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
11
|
|
|
import org.springframework.web.bind.annotation.RestController; |
12
|
|
|
import org.springframework.web.context.request.WebRequest; |
13
|
|
|
|
14
|
|
|
@RestController |
15
|
|
|
class CustomErrorController implements ErrorController { |
16
|
|
|
|
17
|
|
|
private static final String PATH = "/error"; |
|
|
|
|
18
|
|
|
|
19
|
|
|
@Value("${logging.level.org.springframework.web}") |
20
|
|
|
private String loggingLevel; |
21
|
|
|
|
22
|
|
|
@Autowired private ErrorAttributes errorAttributes; |
23
|
|
|
|
24
|
|
|
@RequestMapping(value = PATH) |
25
|
|
|
ErrorDTO error(WebRequest webRequest, HttpServletResponse response) { |
26
|
|
|
// Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. |
27
|
|
|
// Here we just define response body. |
28
|
|
|
return new ErrorDTO(response.getStatus(), getErrorAttributes(webRequest, loggingLevel)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public String getErrorPath() { |
32
|
|
|
return PATH; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private Map<String, Object> getErrorAttributes(WebRequest webRequest, String loggingLevel) { |
36
|
|
|
return errorAttributes.getErrorAttributes(webRequest, "DEBUG".equals(loggingLevel) |
37
|
|
|
? ErrorAttributeOptions.of(ErrorAttributeOptions.Include.STACK_TRACE) : ErrorAttributeOptions.defaults()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|