error-handlingspring-webfluxmongorepository

Spring Webflux: Return statuscode and message based on exception thrown


How to determine which exception is thrown and get the status code out of it in Spring Webflux. This is the structure of my controller code.

@GetMapping("/")
    fun getResults() : Mono<ResponseEntity<AccountDTO>>{
        return Service.getResult()
                .map {                     
                                       
                }.doOnError {
                    //how to get statuscode here
                    throw ResponseStatusException(HttpStatus.NOT_FOUND, it.message!!)
                }

Here I can get the custom message thrown, but how to get the status code? Instead of HttpStatus.NOT_FOUND. I want to capture the status code that is thrown by the service layer. Or is there a way to get the exception thrown?


Solution

  • I found a solution that works. 
    @GetMapping("/")
        fun getResults() : Mono<ResponseEntity<AccountDTO>>{
            return Service.getResult()
                    .map {                     
                                           
                    }.doOnError {
                       if(it is NotFoundException)
                        {
                  
                            throw ResponseStatusException(HttpStatus.NOT_FOUND)
                     
                        }
                        else{
                            throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR)
                        }
                        
                    }