Kamal
Kamal

Reputation: 1476

Custom exception with spring webflux not thrown

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

Upvotes: 3

Views: 1879

Answers (2)

Kamal
Kamal

Reputation: 1476

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

Upvotes: 0

Evgeniy Demidov
Evgeniy Demidov

Reputation: 11

The problem can be solved by the following property configuration:

server.error.include-message: always

Upvotes: 1

Related Questions