Saturday, 31 August 2013

GWT: What to expect when an HTTP request fails

GWT: What to expect when an HTTP request fails

I'm sending a HTTP request with RequestBuilder.send(). I would expect to
see successful responses come back in onResponseReceived(), and errors
come back in onError(). But that's not what I'm seeing. GWT calls
onResponseReceived() regardless of success or failure.
Does anyone know what I should really expect? Do you have any information
that would help me detect and report errors better?
Here's some sample code:
builder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response
response) {
Header[] debugHeaders = response.getHeaders();
String debugHeaders1 = response.getHeadersAsString();
int debugStatusCode = response.getStatusCode();
String debugStatusText = response.getStatusText();
String debugText = response.getText();
handleResponse(response);
}
@Override
public void onError(Request request, Throwable exception) {
reportError();
}
});
I can force an error on my computer by disabling the Wi-Fi. In that case
onResponse() is called. getHeaders() returns an array of length 1, with
the only entry set to null. getHeadersAsString returns "". getStatusCode
returns 0. getStatusText() returns "". getText() returns "".
If this is always the case, I can look for that in my code. However, I
expect that different browsers, different errors, etc, will cause a
different result. Is there any good way to always detect an error?
(As long as there are no HTTP problems, my code works fine.)

No comments:

Post a Comment