25

I am using webflux Mono (in Spring boot 5) to consume an external API. I am able to get data well when the API response status code is 200, but when the API returns an error I am not able to retrieve the error message from the API. Spring webclient error handler always display the message as

ClientResponse has erroneous status code: 500 Internal Server Error, but when I use PostMan the API returns this JSON response with status code 500.

{
 "error": {
    "statusCode": 500,
    "name": "Error",
    "message":"Failed to add object with ID:900 as the object exists",
    "stack":"some long message"
   }
}

My request using WebClient is as follows

webClient.getWebClient()
            .post()
            .uri("/api/Card")
            .body(BodyInserters.fromObject(cardObject))
            .retrieve()
            .bodyToMono(String.class)
            .doOnSuccess( args -> {
                System.out.println(args.toString());
            })
            .doOnError( e ->{
                e.printStackTrace();
                System.out.println("Some Error Happend :"+e);
            });

My question is, how can I get access to the JSON response when the API returns an Error with status code of 500?

Mohale
  • 2,040
  • 3
  • 17
  • 18

4 Answers4

17

If you want to retrieve the error details:

WebClient webClient = WebClient.builder()
    .filter(ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
        if (clientResponse.statusCode().isError()) {
            return clientResponse.bodyToMono(ErrorDetails.class)
                    .flatMap(errorDetails -> Mono.error(new CustomClientException(clientResponse.statusCode(), errorDetails)));
        }
        return Mono.just(clientResponse);
    }))
    .build();

with

class CustomClientException extends WebClientException {
    private final HttpStatus status;
    private final ErrorDetails details;

    CustomClientException(HttpStatus status, ErrorDetails details) {
        super(status.getReasonPhrase());
        this.status = status;
        this.details = details;
    }

    public HttpStatus getStatus() {
        return status;
    }

    public ErrorDetails getDetails() {
        return details;
    }
}

and with the ErrorDetails class mapping the error body

Per-request variant:

webClient.get()
    .exchange()
    .map(clientResponse -> {
        if (clientResponse.statusCode().isError()) {
            return clientResponse.bodyToMono(ErrorDetails.class)
                    .flatMap(errorDetails -> Mono.error(new CustomClientException(clientResponse.statusCode(), errorDetails)));
        }
        return clientResponse;
    })
darrachequesne
  • 742
  • 7
  • 18
10

Look at .onErrorMap(), that gives you the exception to look at. Since you might also need the body() of the exchange() to look at, don't use retrieve, but

.exchange().flatMap((ClientResponse) response -> ....);
Frischling
  • 2,100
  • 14
  • 34
9

Just as @Frischling suggested, I changed my request to look as follows

return webClient.getWebClient()
 .post()
 .uri("/api/Card")
 .body(BodyInserters.fromObject(cardObject))
 .exchange()
 .flatMap(clientResponse -> {
     if (clientResponse.statusCode().is5xxServerError()) {
        clientResponse.body((clientHttpResponse, context) -> {
           return clientHttpResponse.getBody();
        });
     return clientResponse.bodyToMono(String.class);
   }
   else
     return clientResponse.bodyToMono(String.class);
});

I also noted that there's a couple of status codes from 1xx to 5xx, which is going to make my error handling easier for different cases

Mohale
  • 2,040
  • 3
  • 17
  • 18
  • What if my Response class in not String?Will this still work? – Rocky4Ever Aug 24 '20 at 21:05
  • @Rocky4Ever yes you just have to change the expected type, it should work. I am not sure if it will work with primitive data types by the way – Mohale Dec 06 '20 at 18:37
  • `exchange()` is deprecated, but replacing it and `flatMap()` with `exchangeToMono()` seems to work. – Milanka Jul 05 '21 at 07:11
0

I was struggling with this problem too

do this to get body as string

.retrieve()
.onStatus(HttpStatus::isError, clientResponse -> {
               clientResponse.bodyToMono(String.class).blockOptional().ifPresent(log::info);
                return Mono.error(RuntimeException::new);})

This is the solution I found.

EACUAMBA
  • 461
  • 4
  • 8