AngularJS: 1.6.x how to ignore possible unhandled rejection warning in a http interceptor

505 views
Skip to first unread message

Tom Marien

unread,
Feb 15, 2017, 2:37:30 PM2/15/17
to Angular and AngularJS discussion
For instance in following interceptor : (we are using heroku, when we set the maintenance mode to on the router returns 503 status, for which we need to go to a different state (page)

// @ngInject
export function maintenanceInterceptor($q, $injector) {
    return {
        responseError: (response) => {
            if (response.status === 503) {
                if ($injector.has('$state')) {
                    const state = $injector.get('$state');
                    state.go('maintenance', {}, { location: false });
                }
            }
            return $q.reject(response);
        },
    };
}

This now gets logged as a possible unhandled rejection. I've tried

export function maintenanceInterceptor($q, $injector) {
    return {
        responseError: (response) => {
            if (response.status === 503) {
                if ($injector.has('$state')) {
                    const state = $injector.get('$state');
                    state.go('maintenance', {}, { location: false });
                    return $q.reject(response).catch(angular.noop);
                }
            }
            return $q.reject(response);
        },
    };
}

But this results in the $http.then(success, failed) success callback called ??? which is weird

Any advice is much appreciated, i don't want to disable the error by $qProvider.errorOnUnhandledRejections(false);
because we really want to only disable for specific reasons/use-cases

Kr

Sander Elias

unread,
Feb 16, 2017, 8:18:58 AM2/16/17
to Angular and AngularJS discussion
Hi Tom,

Looks like you are using UI-router, I have no experience with that, so I can't advise on that part.

However this:
 return $q.reject(response).catch(angular.noop);

will always return a resolved promise. This line is 1 on 1 the same as:
return $q.resolve(angular.noop)

I don't think that's what you are after.

Regards
Sander


Reply all
Reply to author
Forward
0 new messages