-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathErrorController.java
45 lines (35 loc) · 1.67 KB
/
ErrorController.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
package com.lakesidemutual.policymanagement.interfaces;
import java.util.Map;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* This class implements a custom error controller that returns an <a href=
* "https://www.microservice-api-patterns.org/patterns/quality/qualityManagementAndGovernance/ErrorReport">Error
* Report</a>.
*/
@Controller
public class ErrorController extends AbstractErrorController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public ErrorController(ErrorAttributes errorAttributes) {
super(errorAttributes);
}
@RequestMapping(value = "/error", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> handleError(HttpServletRequest request) {
Map<String, Object> errorAttributes = super.getErrorAttributes(request, ErrorAttributeOptions.of(ErrorAttributeOptions.Include.STACK_TRACE));
Object path = errorAttributes.get("path");
Object status = errorAttributes.get("status");
Object error = errorAttributes.get("error");
Object message = errorAttributes.get("message");
logger.warn("An error occurred while accessing {}: {} {}, {}", path, status, error, message);
return errorAttributes;
}
}