Skip to content

Commit

Permalink
Enable error handling in Java WebClient library, fixes #1243 (#1244)
Browse files Browse the repository at this point in the history
* enable error handling in Java WebClient library, fixes #1243

* remove custom error handling logic in Java WebClient library, fixes #1243
  • Loading branch information
lukstei authored and wing328 committed Feb 19, 2019
1 parent fda867e commit c79d277
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,7 @@ public class ApiClient {
*/
public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames);
return requestBuilder.exchange()
.flatMap(response -> {
HttpStatus statusCode = response.statusCode();
if (response.statusCode() == HttpStatus.NO_CONTENT) {
return Mono.empty();
} else if (statusCode.is2xxSuccessful()) {
if (returnType == null) {
return Mono.empty();
} else {
return response.bodyToMono(returnType);
}
} else {
return Mono.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToMono(returnType);
}

/**
Expand All @@ -514,23 +499,7 @@ public class ApiClient {
*/
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames);
return requestBuilder.exchange()
.flatMapMany(response -> {
HttpStatus statusCode = response.statusCode();
ClientResponse.Headers headers = response.headers();
if (response.statusCode() == HttpStatus.NO_CONTENT) {
return Flux.empty();
} else if (statusCode.is2xxSuccessful()) {
if (returnType == null) {
return Flux.empty();
} else {
return response.bodyToFlux(returnType);
}
} else {
return Flux.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToFlux(returnType);
}

private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,22 +475,7 @@ public MediaType selectHeaderContentType(String[] contentTypes) {
*/
public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames);

return requestBuilder.exchange()
.flatMap(response -> {
HttpStatus statusCode = response.statusCode();
if (response.statusCode() == HttpStatus.NO_CONTENT) {
return Mono.empty();
} else if (statusCode.is2xxSuccessful()) {
if (returnType == null) {
return Mono.empty();
} else {
return response.bodyToMono(returnType);
}
} else {
return Mono.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToMono(returnType);
}

/**
Expand All @@ -511,23 +496,7 @@ public <T> Mono<T> invokeAPI(String path, HttpMethod method, MultiValueMap<Strin
*/
public <T> Flux<T> invokeFluxAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
final WebClient.RequestBodySpec requestBuilder = prepareRequest(path, method, queryParams, body, headerParams, formParams, accept, contentType, authNames);

return requestBuilder.exchange()
.flatMapMany(response -> {
HttpStatus statusCode = response.statusCode();
ClientResponse.Headers headers = response.headers();
if (response.statusCode() == HttpStatus.NO_CONTENT) {
return Flux.empty();
} else if (statusCode.is2xxSuccessful()) {
if (returnType == null) {
return Flux.empty();
} else {
return response.bodyToFlux(returnType);
}
} else {
return Flux.error(new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler"));
}
});
return requestBuilder.retrieve().bodyToFlux(returnType);
}

private WebClient.RequestBodySpec prepareRequest(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames) {
Expand Down

0 comments on commit c79d277

Please sign in to comment.