What is a common way to handle http errors when using jcabi-http client?

109 Views Asked by At

I just started using jcabi's fluent http client and I feel like I am missing some general error handling routines (I believe each jcabi-http user faces it).

So, for a start there's always IOException when I use fetch() or json().readObject() and my first attempt looks like this:

try {
    return new JdkRequest("http://localhost")
            .uri()
                ...
                .back()
            .method(Request.GET)
            .fetch()
            .as(JacksonResponse.class)
                .json().readObject()
                ...;
} catch (IOException e) {
    throw new RuntimeException(e);
}

Next, when response's status in not 200 OK, then json().readObject() fails with the error that "this is not a json you gave me". So I add status check:

try {
    return new JdkRequest("http://localhost")
            ...
            .fetch()
            .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_OK)
            .as(JacksonResponse.class)
                ...;
} catch (IOException e) {
    throw new RuntimeException(e);
}

With this when status is not 200 OK I receive AssertionError, which I have to handle to give it some business meaning:

try {
    return new JdkRequest("http://localhost")
            ...
            .fetch()
            .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_OK)
            .as(JacksonResponse.class)
                ...;
} catch (IOException ex) {
    throw new RuntimeException(ex);
}
} catch (AssertionError error) {
    wrapBusiness(error);
}

Next when I want to get different behavior for 401, 403, 404 of 5xx statuses, then my code will transform into something like this:

try {
    val response = new JdkRequest("http://localhost")
            ...
            .fetch()
            .as(RestResponse.class);
    HttpStatusHandlers.of(response.status()).handle();
    return response
            .as(JacksonResponse.class)
            ...;
} catch (IOException ex) {
    throw new RuntimeException(ex);
}

This "code evolution" looks like a common pattern and I am reinventing the wheel.

Maybe there's an already implemented (or described) solution (or a Wire.class)?

1

There are 1 best solutions below

1
On