0

I'm calling an API which on success returns Employee DTO which looks something like this

{  
   "id": "guid1",
   "name": "John",
   "designation": "Software Engineer"
}

In case of error (4xx, 5xx), it returns an error DTO which returns something like this

{
    "requestId": "guid2", 
    "errorMessage": "some error message",
    "appErrorCode": 1235
} 

I have DTOs for both of these responses.

Now, I am writing WebFlux client using this API

        webClient.post()
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .body(Mono.just(employee), PostEmployeeRequest.class)
                .retrieve()
                .onStatus(
                        httpStatus -> httpStatus == HttpStatus.BAD_REQUEST,
                        clientResponse -> {
                            //  <-- How do I parse the error request body??
                            // Need to write custom logic here, depending upon error response
                           Mono.error(new InvalidInputException(errorMessage));
                        })
                .bodyToMono(PostEmployeeResponseDto.class)
                .block(); 
Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78

1 Answers1

1

You could do

return clientResponse.bodyToMono(MyErrorDto.class).flatMap(errorDTO -> Mono.error(new InvalidInputException(errorDTO)))

inside the .onStatus()

Michael
  • 556
  • 3
  • 12