Search in sources :

Example 1 with ErrorConverter

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);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) JsonObject(io.vertx.core.json.JsonObject) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter)

Example 2 with ErrorConverter

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();
}
Also used : ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) HttpClientResponse(io.vertx.core.http.HttpClientResponse) HttpRequestImpl(io.vertx.ext.web.client.impl.HttpRequestImpl) ErrorConverter(io.vertx.ext.web.client.predicate.ErrorConverter)

Aggregations

ErrorConverter (io.vertx.ext.web.client.predicate.ErrorConverter)2 ResponsePredicate (io.vertx.ext.web.client.predicate.ResponsePredicate)2 Buffer (io.vertx.core.buffer.Buffer)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 HttpRequestImpl (io.vertx.ext.web.client.impl.HttpRequestImpl)1