#ControllerAdvice not throwing exception - spring-boot

I am handling exceptions globally in CustomHandler class. I could see while debugging this class is catching exceptions, but not throwing them to client. Please let me know what i am missing...
#ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
List<String> details = new ArrayList<>();
details.add(ex.getLocalizedMessage());
ErrorResponse error = new ErrorResponse("Server Error", details);
return new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
#ExceptionHandler(ConstraintViolationException.class)
public final ResponseEntity<Object> constraintValidationException(
ConstraintViolationException e) {
List<String> details = new ArrayList<>();
for (ConstraintViolation violation : e.getConstraintViolations()) {
details.add(violation.getMessage());
}
ErrorResponse error = new ErrorResponse("Validation Failed", details);
return new ResponseEntity<Object>(error, HttpStatus.BAD_REQUEST);
}
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<String> details = new ArrayList<>();
for(ObjectError error : ex.getBindingResult().getAllErrors()) {
details.add(error.getDefaultMessage());
}
ErrorResponse error = new ErrorResponse("Validation Failed", details);
return new ResponseEntity(error, HttpStatus.UNPROCESSABLE_ENTITY);
}

Create your own application specific exceptions and handle them. E.g: public class MyAppEx extends RuntimeException {}. Catch the Exception.class and then throw your exception:
try {
// something goes wrong
} catch (Exception e) {
throw new MyAppEx("details of the exception");
}
Replace:
#ExceptionHandler(MyAppEx.class)
public final ResponseEntity<Object> handleAllExceptions(MyAppEx ex, WebRequest request) {..}

Related

Customizing NoHandlerException response with ControllerAdvice

I try to implement a custom error response in a spring boot rest application for 404 errors.
I read many of the solutions presented in stackoverflow, without success.
When I call an invalid entry point I obtain this result:
{
"timestamp": "2022-06-22T10:38:41.114+00:00",
"status": 404,
"error": "Not Found",
"path": "/ws-facturx/fx2"
}
But i'd like to have a response that should look like this:
{
"operationId": "u044eZg2gHwtadqxB5CVv6aeMBjj0w",
"status": "ERROR",
"operation": "webserviceName",
"clientName": "ACME Inc",
"errorMessage": "Error message from Server",
"createdAt": "2022-06-22T09:15:04.844+00:00"
}
I first tried to use #RestControllerAdvice to intercept the exception when they are thrown.
#ExceptionHandler(value = {AppServiceException.class, NoHandlerFoundException.class, ServletServiceException.class })
public ResponseEntity<Object> handleAppServiceException(Exception ex,
WebRequest req) throws JsonProcessingException {
FacturxDto request = context.getFacturxDtoContext();
ErrorMessage errorMessage = errorMessageBuilder(request, ex);
return new ResponseEntity<>(errorMessage, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
I also modified my application.properties :
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false
If i call a non defined entry point I do not reach this method. I tried to use an interceptor.
I firs added a class for adding interceptor to InterceptorRegistry:
#Configuration
public class WebMvcConfig implements WebMvcConfigurer {
private final ApplicationExchangeContext context;
public WebMvcConfig(ApplicationExchangeContext context) {
this.context = context;
}
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ApplicationInterceptor(context)).addPathPatterns("/**");
}
}
My ApplicationInterception looks like this:
#Component
public class ApplicationInterceptor implements HandlerInterceptor {
private final ApplicationExchangeContext context;
#Autowired
public ApplicationInterceptor(ApplicationExchangeContext context) {
this.context = context;
}
//unimplemented methods comes here. Define the following method so that it
//will handle the request before it is passed to the controller.
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (response.getStatus() == HttpStatus.NOT_FOUND.value()) {
// si on a un 404
System.out.println(handler);
String requestData = request.getReader().lines().collect(Collectors.joining());
System.out.println(requestData);
Gson gson = new Gson();
FacturxDto facturxDto = gson.fromJson(requestData, FacturxDto.class);
context.setFacturxDtoContext(facturxDto);
throw new ServletServiceException("404...");
}
System.out.println("Done in preHandle");
return true;
// return HandlerInterceptor.super.preHandle(request, response, handler);
}
#Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
System.out.println(request);
System.out.println(response);
if (response.getStatus() == HttpStatus.NOT_FOUND.value()) {
// si on a un 404
System.out.println(handler);
String requestData = request.getReader().lines().collect(Collectors.joining());
System.out.println(requestData);
Gson gson = new Gson();
FacturxDto facturxDto = gson.fromJson(requestData, FacturxDto.class);
context.setFacturxDtoContext(facturxDto);
throw new ServletServiceException("404...");
}
System.out.println("Done in afterCompletion");
}
}
On the preHandle, i do reach the catch part of the code block but i do not access the RestControllerAdvice method that should handle this exception and build my expected object.
The exception is thrown. But i do not return it to user. Instead I do have an HTML page.

Required parameters exception doesn't work in spring boot 2.x

In my spring boot application, I tried to handled the Required parameter exception. This question may be duplicated. But the answers posted don't help me.
My controller
#GetMapping("/test")
public ObjectId test(#RequestBody OIdLGroupIds OIdLGroupIds,#RequestParam ObjectId _id){
return videoService.test();
}
My global exception handler is like following.
#RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
List<String> details = new ArrayList<>();
details.add(ex.getLocalizedMessage());
ErrorResponse error = new ErrorResponse("Server Error", details);
return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
#ExceptionHandler(NotFoundHandler.class)
public final ResponseEntity<Object> handleRecordNotFoundException(NotFoundHandler ex, WebRequest request) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("message", "Record not found");
List<String> details = new ArrayList<>();
details.add(ex.getLocalizedMessage());
ErrorResponse error = new ErrorResponse("Record Not Found", details);
return new ResponseEntity(error, HttpStatus.NOT_FOUND);
}
#Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDate.now());
body.put("status", status.value());
Set<String> errors = ex.getBindingResult()
.getFieldErrors()
.stream()
.map(x -> x.getDefaultMessage())
.collect(Collectors.toSet());
body.put("details", errors);
return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
}
}
It doesn't throw any details (Body is blank). But the response status is 400 Bad Request. But when I comment all above codes, it throws default exceptions with body.
I tried this also
#Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
String name = ex.getParameterName();
System.out.println(name);
logger.error(name + " parameter is missing");
return super.handleMissingServletRequestParameter(ex, headers, status, request);
}
I have tried in many ways, But no luck. Did I miss anything? Please help me. Thanks in advance.
Empty request body raises HttpMessageNotReadableException.
#RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
#Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
String name = ex.getParameterName();
System.out.println(name);
logger.error(name + " parameter is missing");
return super.handleMissingServletRequestParameter(ex, headers, status, request);
}
#Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(
HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
System.out.println(ex.getMessage());
logger.error("Request body is missing");
return super.handleHttpMessageNotReadable(ex, headers, status, request);
}
}
If you allow empty body, use #RequestBody(required = false).

Spring ConstraintViolation not caught

I'm using Spring Boot, and I have created a custom PathVariable validator, which I use as follows:
#GetMapping(value = "/test/{id}")
public ResponseEntity<Void> test(#PathVariable("id")
#Valid
#MyGUID(message = "ID_INVALID")
String id) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
The validor seems to do its job well - it passes when input is valid, and throws exception when not. The problem is, that my custom exception handler doesn't catch the ConstraintViolationException that is thrown. This is the top line of the stack trace:
javax.validation.ConstraintViolationException: ID_INVALID
Then I have my exeception handler class:
#ControllerAdvice
public class RestExceptionController extends ResponseEntityExceptionHandler {
#ExceptionHandler(value = ConstraintViolationException.class)
public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request) {
List<String> errorDetailsCodes = new ArrayList<>();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
errorDetailsCodes.add(violation.getMessage());
}
ErrorResponse errorResponse = new ErrorResponse(
status.name(),
status.value(),
ex.hashCode(),
errorDetailsCodes
);
logException(ex);
return new ResponseEntity<>(errorResponse, headers, status);
}
#ExceptionHandler(value = Exception.class)
public ResponseEntity<ErrorResponse> handleGeneralException(Exception ex) {
ErrorResponse errorResponse = new ErrorResponse(
HttpStatus.INTERNAL_SERVER_ERROR.name(),
HttpStatus.INTERNAL_SERVER_ERROR.value(),
ex.hashCode()
);
logException(ex);
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
It is strange to me that despite the fact I have both a specific and a general exception handler, they are both skipped, and the system throws its own error:
{
"timestamp": 1568554746642,
"status": 500,
"error": "Internal Server Error",
"message": "ID_INVALID",
"path": "/api/logged-in-user/test/123"
}
How can I catch this exception and return my custom response?
Thanks.

Spring boot Exception custom handling - Unexpected HTTP status

I am trying to implement some custom exception handlers in my spring boot application which will be able to handle custom exceptions and display appropiate message and status code.
My issue : Getting http status = 500 even though the response body is according to my custom handler.
Code :
#ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {
#ExceptionHandler({ BadRequestValidationFailureException.class, Exception.class })
public ResponseEntity<Object> handleAll(Exception ex, WebRequest request) {
ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getMessage());
return new ResponseEntity<Object>( apiError, new HttpHeaders(), HttpStatus.BAD_REQUEST );
}
And throwing exception as :
throw new BadRequestValidationFailureException( "ERROR_CODE", "THIS IS THE MESSAGE" );
The output is :
{
"timestamp": "2018-09-20T17:44:01.502Z",
"status": 500,
"error": "Internal Server Error",
"exception": "com.hotstar.payment.exception.BadRequestValidationFailureException",
"message": "[ ERROR_CODE ] THIS IS THE MESSAGE",
"path": "/my/api/path"
}
The weird thing is that the http response status is 500.
Please help.
Got the solution. Had to set another annotation :
#ResponseStatus(value = HttpStatus.BAD_REQUEST, code = HttpStatus.BAD_REQUEST, reason = "some reason")
Add this to handleAll method.
I've made good experiences with the following pattern:
#ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
private static final Map<MyProjectErrorCode, HttpStatus> CODE_STATUS_MAP = new EnumMap<>(MyProjectErrorCode.class);
static {
CODE_STATUS_MAP.put(MyProjectErrorCode.MYPROJ_ILLEGAL_PROPERTY, HttpStatus.BAD_REQUEST);
CODE_STATUS_MAP.put(MyProjectErrorCode.MYPROJ_FOO, HttpStatus.BAD_REQUEST);
CODE_STATUS_MAP.put(MyProjectErrorCode.MYPROJ_THIRDPARTYX_CLIENT, HttpStatus.INTERNAL_SERVER_ERROR);
CODE_STATUS_MAP.put(MyProjectErrorCode.MYPROJ_UNKNOWN, HttpStatus.INTERNAL_SERVER_ERROR);
CODE_STATUS_MAP.put(MyProjectErrorCode.THIRDPARTYX_BAR, HttpStatus.BAD_REQUEST);
CODE_STATUS_MAP.put(MyProjectErrorCode.THIRDPARTYX_UNKNOWN, HttpStatus.INTERNAL_SERVER_ERROR);
}
#ExceptionHandler(MyProjectException.class)
public ResponseEntity<ErrorResponse> handleMyProjectException(MyProjectException ex) {
ErrorResponse errorResponse = createErrorResponse(ex.getErrorCode(), ex.getMessage());
HttpStatus httpStatus = determineHttpStatus(ex.getErrorCode());
return handleErrorResponse(errorResponse, httpStatus);
}
#ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex) {
MyProjectErrorCode errorCode = MyProjectErrorCode.MYPROJ_ILLEGAL_PROPERTY;
ErrorResponse errorResponse = createErrorResponse(errorCode, ex.getMessage());
HttpStatus httpStatus = determineHttpStatus(errorCode);
return handleErrorResponse(errorResponse, httpStatus);
}
#ExceptionHandler(RuntimeException.class)
public ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException ex) {
MyProjectErrorCode errorCode = MyProjectErrorCode.MYPROJ_UNKNOWN;
ErrorResponse errorResponse = createErrorResponse(errorCode, ex.getMessage());
HttpStatus httpStatus = determineHttpStatus(errorCode);
return handleErrorResponse(errorResponse, httpStatus);
}
private ResponseEntity<ErrorResponse> handleErrorResponse(ErrorResponse errorResponse, HttpStatus httpStatus) {
return new ResponseEntity<>(errorResponse, httpStatus);
}
private ErrorResponse createErrorResponse(MyProjectErrorCode errorCode, String message) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setErrorCode(errorCode.name());
errorResponse.setErrorMessage(message);
return errorResponse;
}
private HttpStatus determineHttpStatus(MyProjectErrorCode errorCode) {
return CODE_STATUS_MAP.getOrDefault(errorCode, HttpStatus.INTERNAL_SERVER_ERROR);
}
The client can get the HttpStatus from the Http response - no need to add it to the JSON body.
The project specific MyProjectErrorCode enum allows you to communicate to the clients a detailed error code. The client can analyze this error code and take the appropriate action or display a localized (specific or generic) error message based on the error code.
MyProjectErrorCode also allows you to communicate if the error was created in your code (starting with MYPROJ_) or if the error is forwarded from the third party 'x' service (starting with THIRDPARTYX_).
You can also create subclasses of MyProjectException and ErrorResponse to transport more specific data for specific cases - just add an additional expcetion handler method for that exception.

Spring - Manage custom Exception page

I'm trying to manage a custom error page with my custom exception.
I have this exception
#ResponseStatus(value=HttpStatus.NOT_FOUND, reason="Inesistente")
public class ResourceNotAccessibleException extends Throwable{
public ResourceNotAccessibleException(String message){
super(message);
}
}
which i want to respond with a 404 error.
Than i'm managing an error controller
#ControllerAdvice
public class ErrorController {
#ExceptionHandler({ResourceNotAccessibleException.class})
public ModelAndView getErrorPage(HttpServletRequest request, Throwable ex) {
String errorMsg = "";
int httpErrorCode = getErrorCode(request);
switch (httpErrorCode) {
case 404: {
logger.error("Status Error " + httpErrorCode , ex.getMessage());
errorMsg = messageSource.getMessage("errorMessage", new Object[] { uuid, +httpErrorCode }, locale);
break;
}
case 400: {
errorMsg = "BAD REQUEST";
break;
}
case 500: {
errorMsg = messageSource.getMessage("errorMessage", new Object[] { uuid, +httpErrorCode }, locale);
logger.error("Status Error " + httpErrorCode , ex.getMessage());
break;
}
}
ModelAndView mav = new ModelAndView();
mav.addObject("errorMsg", errorMsg);
mav.setViewName("error");
return mav;
}
Now, in my controller if i have something like
if(object==null) {
throw new ResourceNotAccessibleException("Resource does not exist");
}
I should see my error view, but i'm getting the classic white error page, in my log i see the exception being hit..
The ResourceNotAccessibleException should extend Exception or RuntimeException and not Throwable. More info
If you can't change exception type, probably you could try ExceptionHandlerExceptionResolver or this awesome post about Spring exception handling
One more thing, you probably want to add some #ResponseStatus info above getErrorPage, because you are handling this exeption and #ResponseStatus annotation above the ResourceNotAccessibleException will never trigger.
So i think something like this should work:
#ControllerAdvice
public class ErrorController {
#ResponseStatus(value= HttpStatus.NOT_FOUND) // <= important
#ExceptionHandler({ResourceNotAccessibleException.class})
public ModelAndView getErrorPage(HttpServletRequest request, Throwable ex) {
String errorMsg = "";
// ... some code here
ModelAndView mav = new ModelAndView();
mav.addObject("errorMsg", errorMsg);
mav.setViewName("error");
return mav;
}
}
public class ResourceNotAccessibleException extends Exception{ // <= important
public ResourceNotAccessibleException(String message){
super(message);
}
}
If this doesn't work, you can also try to change resource view file name to something like errorPage.jsp or errorPage.html and set it like mav.setViewName("errorPage");
You need to replace the default error pages in your web container and map a status code to a particular error page.
Here are the changes you need to make:
If it's a Jetty container, here are the changes:
#Bean
public JettyEmbeddedServletContainerFactory
containerFactory(
#Value("${server.port:8080}") final String port,
#Value("${jetty.threadPool.maxThreads:600}") final String maxThreads,
#Value("${jetty.threadPool.minThreads:10}") final String minThreads,
#Value("${jetty.threadPool.idleTimeout:5000}") final String idleTimeout) {
final JettyEmbeddedServletContainerFactory factory =
new JettyEmbeddedServletContainerFactory(Integer.valueOf(port));
...
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
"/error-info.html"));
...
return factory;
}
If it's a Tomcat container, here are the changes:
#Bean
public EmbeddedServletContainerCustomizer container() {
return new EmbeddedServletContainerCustomizer() {
#Override
public void customize(
ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new
ErrorPage(HttpStatus.NOT_FOUND, "/error-info.html"));
}
};
}
For your ErrorController, don't set view name. It will pick the view from the error page mapping which was set earlier.
#ControllerAdvice
public class ErrorController {
#ExceptionHandler(ResourceNotAccessibleException.class)
#ResponseStatus(value = HttpStatus.NOT_FOUND)
public ModelAndView handleResourceNotAccessibleException(
HttpServletRequest req, ResourceNotAccessibleException ex) {
...
ModelAndView mav = new ModelAndView();
mav.addObject("errorMsg", errorMsg);
retrun mav;
}
}
Location of error-info.html or jsp under resources/static

Resources