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

$http incorrectly reports back 404 on canceled request #6074

Closed
langdonx opened this issue Jan 31, 2014 · 11 comments
Closed

$http incorrectly reports back 404 on canceled request #6074

langdonx opened this issue Jan 31, 2014 · 11 comments

Comments

@langdonx
Copy link

As of 1.2.8, canceled $http requests return a status of 404, instead of 0. It's a little tricky to reproduce on a public fiddle, but here's how:

Load the following full-screen fiddle and just keep trying to refresh the page before the page has fully loaded (F5, F5, F5, F5...). Eventually you'll see an alert that says "error callback 404":

http://jsfiddle.net/C4jRt/8/embedded/result/

In 1.2.7 and before, you'll get "error callback 0" as expected:

http://jsfiddle.net/C4jRt/7/embedded/result/

Chrome and Firefox give 404, so it's not browser specific. Firefox swallows the alert when you refresh, so you have to be quick to see it. I couldn't ever see the alert in IE, so.

I'm relying on this status code to handle and react to errors. My web server is a little slow, so I don't want to show a bunch of errors to the user when they refresh while they're waiting for it to come back.

v1.2.8 (incorrect)
http://jsfiddle.net/C4jRt/8

v1.2.7 (correct)
http://jsfiddle.net/C4jRt/7

@caitp
Copy link
Contributor

caitp commented Jan 31, 2014

I can't reproduce this on Chrome32 or FF29

@benjamingr
Copy link
Contributor

I've written a more isolated test case, I can not seem to reproduce, getting 0 on Chrome 32

var $http = angular.injector(["ng"]).get("$http");

$http.get('/echo/json', {timeout: 1, tested: true}).success(function(el){
    console.log(response);
}).error(function(err,status){
    console.log("Err",err,status);    
});

Logs Err null 0

http://jsfiddle.net/5BVhD/

@langdonx
Copy link
Author

@benjamingr Timeout doesn't have the status code issue. It's the cancelling of the XHR through refreshing the browser that causes the problem.

@caitp I've reproduced it on my site with a slow-running node.js service. Load the page and click refresh within 2 seconds.

http://home.langdonx.com/angular-http-test/test-1.2.1.html (gives status code 0)
http://home.langdonx.com/angular-http-test/test-1.2.7.html (gives status code 0)
http://home.langdonx.com/angular-http-test/test-1.2.8.html (incorrectly gives status code 404)
http://home.langdonx.com/angular-http-test/test-1.2.10.html (incorrectly gives status code 404)

The node service is as follows:

http.createServer(function (req, res) {
    var u = url.parse(req.url, true);
    setTimeout(function () {
        res.writeHead(200, {'Content-Type': 'application/json'});
        res.end('{"complete": true}');
    }, parseInt(u.query.value));
}).listen(process.env.PORT);

(Please be kind to my server ;))

It's easily reproducible on Chrome 32, Chrome 34, and Firefox 26. IE seems to just shut everything down on refresh.

@chriswep
Copy link

chriswep commented Feb 3, 2014

i can confirm this problem. a request that fails via config.timeout works as expected with a status code of 0. if there is a browser(?) timeout (Chrome Canary here on OSX Mavericks, no answer from server after 90 seconds) the status code is 404 which is wrong.

@swallez
Copy link

swallez commented Feb 6, 2014

Could this be related to 28fc80b where a status code 0 is changed to a 404 because of a bug in Android?
Practically this means we can't distinguish server down from a real 404.

@langdonx
Copy link
Author

It's interesting to note that there are positive benefits of this false 404.

When CORS requests fail in 1.2.7, they fail silently and the $http.error callback will be called, so my app is stuck in a wait state.

In 1.2.10 specifically (but probably in 1.2.8+), the $http.error receives an incorrect 404, and my app responds to it and sends the user to the error page.

It would be ideal to be able to detect the request failure if it failed because of CORS, but I'm not sure if it's possible.

@Francisc
Copy link

What's the status with this issue?

@marcalj
Copy link

marcalj commented Mar 1, 2014

Duplicated issue, sorry :p #6503

pkozlowski-opensource added a commit to pkozlowski-opensource/angular.js that referenced this issue Mar 1, 2014
PR angular#5547 introduced conversion of all 0 status codes to 404 for cases
where no response was recieved (previously this was done for the
file:// protocol only). But this mechanism is too eager and
masks legitimate cases where status 0 should be returned. This commits
reverts to the previous mechanism of handling 0 status code for the
file:// protocol (converting 0 to 404) while retaining the returned
status code 0 for all the protocols other than file://

Fixes angular#6074
Fixes angular#6155
pkozlowski-opensource added a commit that referenced this issue Mar 14, 2014
PR #5547 introduced conversion of all 0 status codes to 404 for cases
where no response was recieved (previously this was done for the
file:// protocol only). But this mechanism is too eager and
masks legitimate cases where status 0 should be returned. This commits
reverts to the previous mechanism of handling 0 status code for the
file:// protocol (converting 0 to 404) while retaining the returned
status code 0 for all the protocols other than file://

Fixes #6074
Fixes #6155
@allaLad
Copy link

allaLad commented Jun 10, 2014

This fix is part of AngularJS v1.3.0-beta.11.
Is it possible to get v1.2 be patched with the fix?
Thanks

@langdonx
Copy link
Author

@allaLad I'm seeing the fix in 1.2.17, are you not?

@allaLad
Copy link

allaLad commented Jun 10, 2014

I haven't seen it, it was released 2 days ago, we're using v1.2.9. Thanks!

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