3
votes

I'm trying to throw a custom exception when Resource is not found, somehow the custom exception is overridden by the SpringFramework and custom message is not shown. below is the a custom exception I have written

@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class SkeletonNotFoundException extends RuntimeException {
    public SkeletonNotFoundException(String message) {
       super(message);
    } 
}

I want to throw above exception when Mono not found, below is the logic of finding A resource and throwing the error

@GetMapping(value = "/{skeletonId}")
public Mono<ResponseEntity<?>> getSkeleton(@PathVariable final Long skeletonId) {
    return skeletonService.findById(skeletonId)
            .switchIfEmpty(Mono.error(new SkeletonNotFoundException("Skeleton not found")))
            .map(skeleton -> this.skeletonMapper.mapToDto(skeleton))
            .map(body -> ResponseEntity.ok().body(body));
}

Below is the result i get from postman when I send request to the endpoint

{
"timestamp": "2020-11-29T06:15:26.935+00:00",
"path": "/api/v1/skeletons/3",
"status": 404,
"error": "Not Found",
"message": "",
"requestId": "2094d9b0-1"
}

I can't see my custom message, somehow another layer is overriding the CustomException

2
To rule out any other problem, I recommend developing (even temporarily) a custom ErrorWebExceptionHandler. In that way, you will be sure the returned 404 is related or not with such exception. Several examples about how to do it: baeldung.com/spring-webflux-errors and stackoverflow.com/questions/47958622/… - doctore
I was able to solve it through the @RestControllerAdvice, I'll check the the link you have pointed out - Kamal
the exception handling provided from Baeldung is a bad example and should not be used. There is reason as to why comments on that article is closed. Instead the exception handling provided in the official documentation is the one that should be looked at. If using annotated controllers you can use the controller advice annotations and handle exceptions like any spring boot application. As mentioned here docs.spring.io/spring-framework/docs/current/reference/html/… - Toerktumlare
A custom WebExceptionHandler is only needed if you need to handle exceptions that are thrown before a handler is chosen, so for instance exceptions that are thrown in WebFilters. Which is clearly pointed out in the stack overflow linked example. - Toerktumlare
@Kamal my proposal was to rule out any other previous problem. But if you have tested using @RestControllerAdvice is clear the problem is in "other part". Not sure the Webflux version you are using, there was a problem related with it: github.com/spring-projects/spring-boot/pull/19901 I just created a dummy project with the latest one, returning Mono.empty() in skeletonService.findById(skeletonId)and it works as expected, that is, Skeleton not found was included in the response message property. - doctore

2 Answers

1
votes

The problem can be solved by the following property configuration:

server.error.include-message: always
0
votes

I have used Spring's @RestControllerAdvice to show custom exceptions