use of io.vertx.ext.web.client.predicate.ErrorConverter in project vertx-web by vert-x3.
the class WebClientExamples method predicateCustomErrorWithBody.
public void predicateCustomErrorWithBody() {
ErrorConverter converter = ErrorConverter.createFullBody(result -> {
// Invoked after the response body is fully received
HttpResponse<Buffer> response = result.response();
if (response.getHeader("content-type").equals("application/json")) {
// Error body is JSON data
JsonObject body = response.bodyAsJsonObject();
return new MyCustomException(body.getString("code"), body.getString("message"));
}
// Fallback to defaut message
return new MyCustomException(result.message());
});
ResponsePredicate predicate = ResponsePredicate.create(ResponsePredicate.SC_SUCCESS, converter);
}
use of io.vertx.ext.web.client.predicate.ErrorConverter in project vertx-web by vert-x3.
the class PredicateInterceptor method handle.
@Override
public void handle(HttpContext<?> httpContext) {
if (httpContext.phase() == ClientPhase.RECEIVE_RESPONSE) {
// Run expectations
HttpRequestImpl request = (HttpRequestImpl) httpContext.request();
HttpClientResponse resp = httpContext.clientResponse();
List<ResponsePredicate> expectations = request.expectations;
if (expectations != null) {
for (ResponsePredicate expectation : expectations) {
ResponsePredicateResultImpl predicateResult;
try {
predicateResult = (ResponsePredicateResultImpl) expectation.apply(responseCopy(resp, httpContext, null));
} catch (Exception e) {
httpContext.fail(e);
return;
}
if (!predicateResult.succeeded()) {
ErrorConverter errorConverter = expectation.errorConverter();
if (!errorConverter.requiresBody()) {
predicateResult.setHttpResponse(responseCopy(resp, httpContext, null));
failOnPredicate(httpContext, errorConverter, predicateResult);
} else {
resp.bodyHandler(buffer -> {
predicateResult.setHttpResponse(responseCopy(resp, httpContext, buffer));
failOnPredicate(httpContext, errorConverter, predicateResult);
});
resp.resume();
}
return;
}
}
}
}
httpContext.next();
}
Aggregations