ArcBees / gwtquery

A jQuery clone for GWT, and much more.
MIT License
85 stars 38 forks source link

Promise.fail(..) should return the original error #359

Open lisboa opened 8 years ago

lisboa commented 8 years ago

When an error occurr, the exception can be obtained using

final RequestException exception = (RequestException) getArgument(4);

But the exception is not the original error, because the text "HTTP ERROR: 500 Internal Server Error" is prepended. Therefore, if the original error is {message:"my message"}, the RequestException.getMessage() will return

HTTP ERROR: 500 Internal Server Error {message:"my message"}. 

Then I cannot convert easily to a json object to get the error.

* Another improvement is to provide access to the original response from the server, which would give more options even to circumvent problems. For example, in this particular case, the problem could be overcome using response.getText () instead of exception.getMessage().

The code bellow:

public void onResponseReceived(Request request, Response response) {
    int status = response.getStatusCode();
    if (status <= 0 || status >= 400) {
      String statusText = status <= 0 ? "Bad CORS" : response.getStatusText();
      onError(request, new RequestException("HTTP ERROR: " + status + " " + statusText + "\n"
          + response.getText()));
    } else {
      dfd.resolve(response, request);
    }
  }

Could be change to (note the response object as second param of onError call):

public void onResponseReceived(Request request, Response response) {
    int status = response.getStatusCode();
    if (status <= 0 || status >= 400) {
      String statusText = status <= 0 ? "Bad CORS" : response.getStatusText();
     onError(request, response, new RequestException("HTTP ERROR: " + status + " " + statusText + "\n"
          + response.getText()));
    } else {
      dfd.resolve(response, request);
    }
  }

Thanks, Fabio.