Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($http,$resource): statusText resolved in timeout promise #14021

Closed
wants to merge 3 commits into from

Conversation

mrac
Copy link
Contributor

@mrac mrac commented Feb 12, 2016

Timeout-promise resolved value will be passed as response error statusText.

  var promise = $timeout(function() {
    return "something";
  }, 500);

  $http({
    method: 'GET',
    url: '...',
    timeout: promise
  }).catch(function(err) {
    console.log(err.status);  // -1
    console.log(err.statusText);  // something
  });

This feature will also help distinguish cancelled $resource requests
from timed-out $resource requests. Before, both cases resulted
with the same error object:
{status: -1, statusText: '', ...}
thus impossible to handle both cases in different way.

Now cancelled requests will come with statusText='cancelled',
and timed-out requests with statusText='timeout' in the error object.

Timeout-promise resolved-value will be passed as response error statusText

```js
  $http({
    method: 'GET',
    url: '...',
    timeout: $timeout(function() { return "something"; }, 500)
  }).catch(function(err) {
    console.log(err.statusText);  // something
  });
```

This feature will also help distinguish cancelled $resource requests
from timed-out $resource requests. Before both cases resulted
with the same error object:
```{status: -1, statusText: '', ...}```
and were not giving much options to handle them in different way.

Now cancelled requests will come with statusText='cancelled' and
timed-out requests with statusText='timeout' in error object.
for (var i = 0, ii = responses.length; i < ii; i++) {
if (responses[i] === handleResponse) {
responses.splice(i, 1);
callback(-1, undefined, '');
if (statusText) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just always call callback(-1, undefined, '', statusText) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because some unit tests use spy's toHaveBeenCalledWith() method which is strict regarding the number of parameters, and I didn't want to break the old API. With undefined as the forth parameter those tests will fail, so may be some third-party modules would break if they test for it. It's minor however, and if you agree, we can just change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The advice in the docs is that "you should never need to use this service directly" and it's use as a function isn't even documented (even more so the arguments of the done callback).
I really think it's a non-issue, so go ahead and use the simpler version 😄

@gkalpak
Copy link
Member

gkalpak commented Feb 13, 2016

Overall looks good. I left a couple of comments.

@gkalpak
Copy link
Member

gkalpak commented Jul 6, 2016

@mrac , you never pinged me and I missed your latest change.
LGTM (expect that this needs to be rebased and the test have to be converted to Jasmine 2).

@Narretz
Copy link
Contributor

Narretz commented Jul 6, 2016

Is this a BC? The statusText is now something that it wasn't before in some cases. Could break some logic / tests.

@gkalpak
Copy link
Member

gkalpak commented Jul 6, 2016

In that sense it can be thought of as a BC, I guess.

@Narretz
Copy link
Contributor

Narretz commented Apr 6, 2018

We have added the xhrStatus to the response in the 1.6.x branch.

@Narretz Narretz closed this Apr 6, 2018
@nicolas-berezin
Copy link

@Narretz @gkalpak
Hey guys,
Seems like this issue still exists on the latest AngularJS build.
If ngResource is configured as:
$resource("url", {}, { list: { method: 'GET', cancellable: true, timeout: 1000 }
So if it is cancellable and has a numeric timeout at the same time, the response object will always have
xhrStatus: 'abort' even if the request was cancelled by timeout.
There is no way to distinguish was it cancelled by calling $cancelRequest() or it was cancelled by timeout.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants