Skip to content

Extend WebClient with createException(HttpStatus code) #24126

Closed
@membersound

Description

@membersound

It would be nice if you could extend WebClient with createException(HttpStatus code).

Because eg I have an external webservice that I have no control of, and it always returns 200 OK EVEN in case of error responses in body and an error hint in the header.

So when I evaluate the response with a ExchangeFilterFunction, I might want to return a clientResponse.createException(BAD_GATEWAY) based on the received content.

This is impossible by simply calling clientResponse.createException(), which would create a json exception, BUT with status 200 OK proxied through!

In my case, I'd like to write as follows:

private static ExchangeFilterFunction errorHandler() {
	return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
                  HttpHeaders headers = clientResponse.headers().asHttpHeaders();
                  return (headers.getFirst("error").equals("true")) 
			? clientResponse.createException(BAD_GATEWAY).flatMap(ex -> Mono.error(ex)) //this is not possible atm
			: Mono.just(clientResponse);
	});
}

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions