Julian Jelfs’ Blog

Angular auto-retry interceptor for WinInet error codes

Posted in angular, Javascript by julianjelfs on June 5, 2015

It’s a little known fact that IE9 (and maybe lower versions if you have to support them) will occasionally return strange looking codes from xhr requests. You probably won’t be handling these in your angular app because they are not http codes. But they do happen and they happen quite often.

I have found that simply retrying the request (just once) will all but eradicate this problem. You might also consider a single retry on any xhr error.

How do you actually do this? Using an http interceptor as follows is the most generic way I have found.

'use strict';

angular
    .module('app')
    .config(($provide, $httpProvider) => {

    var codesToRetry = [12152,12002,12031,12029,12030,12041,12019];

    $provide.factory('retryInterceptor', ($injector, $q) => {
        return {
            'responseError' : (resp) => {
                if(codesToRetry.indexOf(resp.status) >= 0 &&
                    resp.config.headers['X-Auto-Retry'] == null) {
                    resp.config.headers['X-Auto-Retry'] = resp.status;
                    var $http = $injector.get('$http');
                    return $http(resp.config);
                } else {
                    return $q.reject(resp);
                }
            }
        };
    });

    if ($httpProvider.interceptors.indexOf('retryInterceptor') < 0) {
        $httpProvider.interceptors.push('retryInterceptor');
    }
});

 

Here I have just picked a handful of codes that I saw in my own logs to automatically retry. We ensure that we only retry once by inserting a custom retry header to check on subsequent failures.

Hope this is useful to someone.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: