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

$http timeout 408 #10081

Open
Open
@jimmywarting

Description

@jimmywarting

I just used the timeout option in $http config cuz i thought it were taking to long for a simple get request to timeout on the server

$http.get("https://example.com", {
    timeout: 10000
}).catch(function(res){
    console.log(res.status); // 0
    // Here I have no idea if it timed out cuz 0 is a undocumented status code it can mean more things
    // I want to treat it the same as 408 (request timeout) as it came from the server
});

The usecase is to show a message depending on the code as in ng-switch on="res.status"

Instead i have to use $timeout and a promise instad of a simple integer and set the status code for myself

var timedout = false;
var timer = $timeout(function(){
    timedout = true;
}, 10000);

$http.get("https://example.com", {
    timeout: timer
}).catch(function(res){
    if(timedout) res.status = 408
}).finaly(function(){
    $timeout.cancel(timer);
});

Now a config with {timeout: promise} can mean anything and be canceled for any reason like "not allowed to make so many request" or whatever. But with a {timeout: 10000} we could assume it just timed out and we can set the status code to 408.

Would it make since to emulate 408 or don't we do any special treatment per request?

With simple xhr its much easier to tell if it timedout cuz you have the xhr.ontimeout event

Activity

changed the title [-]timeout[/-] [+]$http timeout 408[/+] on Nov 16, 2014
jimmywarting

jimmywarting commented on Nov 16, 2014

@jimmywarting
ContributorAuthor

This lead me to another questions...
xhr.timeout = [unsigned long]
xhr.timeout can be used to cancel a requests after x seconds. $http is not using this and instead it uses a setTimeout-promise then xhr.abort. Would this be treated as the same?

  1. Request get queued when making more then 6 request to the same origin... is xhr.timeout starting the countdown directly for the queued request or when request has been sent? the $http setTimeout-promise starts the countdown directly...
  2. and would the $http timeout take longer then xhr.timeout if I would make the tab idle by going to another tab those makeing the setTimeout slower?
  3. Wouldn't it be better to use xhr.timeout instead?
added this to the Ice Box milestone on Nov 17, 2014
pkozlowski-opensource

pkozlowski-opensource commented on Nov 17, 2014

@pkozlowski-opensource
Member

I don't know how I feel about this one... Somehow what you are saying makes sense. At the same time I don't think we should be impersonating a back-end. Let's see what others got to say... we could do it, technically speaking... if there is enough interest in the change.

petebacondarwin

petebacondarwin commented on Nov 17, 2014

@petebacondarwin
Contributor

I think you have a reasonable point here. It is a bit of a pain to ensure that you know that the failure was due to a timeout.

While using xhr.timeout and xhr.ontimeout might be a good way forward (I need to find out why it wasn't used in the first place), we could possibly solve this by a small modification to $httpBackend. Currently a $http timeout (i.e. number or promise) will trigger xhr.abort(). This is the only bit of the $httpBackend that does this. We handle this abort here:

https://github.com/angular/angular.js/blob/master/src/ng/httpBackend.js#L89

xhr.onabort = requestError;

Perhaps we could provide a different handler than requestError to handle the abort?

modified the milestones: Backlog, Ice Box on Nov 17, 2014
jimmywarting

jimmywarting commented on Nov 17, 2014

@jimmywarting
ContributorAuthor

@petebacondarwin Perhaps it was when we supported IE 7 that we didn't do a xhr.timeout? It was introduced in IE8 if I'm not mistaking.

@pkozlowski-opensource I respect your decision to impersonating a back-end or not

realityking

realityking commented on Nov 17, 2014

@realityking
Contributor

I started playing around with xhr.timeout when I refactored some stuff, unfortunately it bloated the code quote a bit since all the existing code has to be kept for JSONP requests. I'll dust of my branch since there seems to be some interest.

floofydoug

floofydoug commented on Feb 25, 2016

@floofydoug

@jimmywarting was there a consensus reached about whether an integer would be supported? In the most recent angular docs I see that number | promise could be used, however, I could only get the timeout to work if I wrapped it in a promise like your initial post.

EDIT: Sorry. I was expecting status code 408, but was getting 0. It is working as expected.

leonardobazico

leonardobazico commented on Mar 31, 2016

@leonardobazico

+1

1 remaining item

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @petebacondarwin@realityking@pkozlowski-opensource@jimmywarting@floofydoug

        Issue actions

          $http timeout 408 · Issue #10081 · angular/angular.js