Completed
Push — master ( 474fe9...e97e5f )
by Valentyn
20:24 queued 40s
created

error(WebRequest,HttpServletResponse)   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
eloc 3
rs 10
cc 1
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.servlet.error.ErrorAttributes;
8
import org.springframework.boot.web.servlet.error.ErrorController;
9
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.RestController;
11
import org.springframework.web.context.request.WebRequest;
12
13
@RestController
14
class CustomErrorController implements ErrorController {
15
16
    private static final String PATH = "/error";
0 ignored issues
show
introduced by
Refactor your code to get this URI from a customizable parameter.
Loading history...
17
18
    @Value("${logging.level.org.springframework.web}")
19
    private String loggingLevel;
20
21
    @Autowired
22
    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
    @Override
32
    public String getErrorPath() {
33
        return PATH;
34
    }
35
36
    private Map<String, Object> getErrorAttributes(WebRequest webRequest, String loggingLevel) {
37
        return errorAttributes.getErrorAttributes(webRequest, "DEBUG".equals(loggingLevel));
38
    }
39
40
}
41