Skip to content

Provide access to the URL or HttpRequest from the HttpReponse #462

@gtaylor1981

Description

@gtaylor1981

I'm writing a generic error handling method to pass to ifFailure() and I want to log the URL where the error came from.

public static <T> Consumer<HttpResponse<T>> createErrorHandler(String source) {
    return httpResponse -> {
        LOG.warn(source + ": Error code " + httpResponse.getStatus() +" '" 
                        + httpResponse.getStatusText() + "' when querying URL " + "???");
    };
}

In my error handler where it says "???" above I would like to be able to do something like httpResponse.getUrl() or even httpResponse.getRequest().getUrl().

Simplified, I use the error handler like

var getResponse = Unirest.get().queryString("a", "x").asString().ifFailure(createErrorHandler("MyGetQuery"));
var postResponse = Unirest.post().body(jsonObject).asString().ifFailure(createErrorHandler("MyPostQuery"));

I can't see another way to do this, other than always passing the URL to the handler. As this is created dynamically I then have to break up the fluent API as I have to first create the request separately, then pass this to the error handler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions