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.
I'm writing a generic error handling method to pass to
ifFailure()and I want to log the URL where the error came from.In my error handler where it says
"???"above I would like to be able to do something likehttpResponse.getUrl()or evenhttpResponse.getRequest().getUrl().Simplified, I use the error handler like
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.