3

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

Kamal
  • 1,476
  • 1
  • 13
  • 21
  • 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: https://www.baeldung.com/spring-webflux-errors and https://stackoverflow.com/questions/47958622/spring-security-webflux-reactive-exception-handling – doctore Nov 29 '20 at 11:49
  • 1
    I was able to solve it through the @RestControllerAdvice, I'll check the the link you have pointed out – Kamal Nov 29 '20 at 12:37
  • 1
    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 https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-ann-rest-exceptions – Toerktumlare Nov 29 '20 at 16:00
  • 1
    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 Nov 29 '20 at 16:01
  • 1
    @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: https://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 Dec 01 '20 at 17:28
  • Excellent doctor , I'm using spring-boot version 2.4.0 which has spring-webflux 5.3.1 – Kamal Dec 01 '20 at 18:41

2 Answers2

1

The problem can be solved by the following property configuration:

server.error.include-message: always
0

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

Kamal
  • 1,476
  • 1
  • 13
  • 21