ErrorController implements org.springframework.boot.autoconfigure.web.ErrorController { @RequestMapping("/error") public ResponseEntity<Map<String, Object>> error(HttpServletRequest aRequest) { RequestAttributes requestAttributes = new ServletRequestAttributes(aRequest); Map<String, Object> result = this.errorAttributes.getErrorAttributes(requestAttributes, false); Throwable error = this.errorAttributes.getError(requestAttributes); if (error instanceof MethodArgumentNotValidException) { BindingResult bindingResult = ((MethodArgumentNotValidException) error).getBindingResult(); if (bindingResult.hasErrors()) { Map<String, String> details = bindingResult.getAllErrors().stream().filter(e -> e instanceof FieldError) .map(e -> (FieldError) e).collect(toMap(FieldError::getField, FieldError::getDefaultMessage)); result.put("details", details); } } else if (error instanceof PdpPolicyException) { PdpPolicyException e = (PdpPolicyException) error; result.put("details", e.getDetails()); } if (result.containsKey("details")) { result.remove("exception"); result.remove("message"); } HttpStatus statusCode; if (error == null) { statusCode = result.containsKey("status") ? HttpStatus.valueOf((Integer) result.get("status")) : INTERNAL_SERVER_ERROR; } else { ResponseStatus annotation = AnnotationUtils.getAnnotation(error.getClass(), ResponseStatus.class); statusCode = annotation != null ? annotation.value() : INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(result, statusCode); } @Autowired  ErrorController(ErrorAttributes errorAttributes); @Override String getErrorPath(); @RequestMapping("/error") ResponseEntity<Map<String, Object>> error(HttpServletRequest aRequest);  }
@Test public void testErrorWithForbiddenAnnotated() throws Exception { HttpServletRequest request = new MockHttpServletRequest(); when(errorAttributes.getError(any())).thenReturn(new PolicyIdpAccessMismatchIdentityProvidersException("test")); ResponseEntity<Map<String, Object>> response = subject.error(request); assertEquals(FORBIDDEN, response.getStatusCode()); Map<String, Object> body = response.getBody(); assertTrue(body.containsKey("exception")); assertTrue(body.containsKey("message")); }