alexcrack / angular-ui-notification

Angular.js service providing simple notifications using Bootstrap 3 styles with css transitions for animating
MIT License
536 stars 169 forks source link

notification can not be injected to httpInterceptor #138

Open quchwe opened 6 years ago

cristian-milea commented 6 years ago

hi. i used this approach, i don't know if it's the best

$httpProvider.interceptors.push(function ($q, $rootScope, $cookies, env, $injector) {
        let errorNotify = (err) => {
            var Notification = $injector.get('Notification');
            if(err.error && angular.isArray(err.error)){
                angular.forEach( err.error, (text) => {
                    if(text.indexOf('_') !== -1) text = text.replace(/_/g, ' ');
                    Notification.error(text);
                })  
            } else if(err.error){
                Notification.error(err.error);
            } else if(err) {
                Notification.error(err);
            }
        }
return {
.
.
.
'responseError': function (rejection) {
                if (parseInt(rejection.status) === 401) {
                    $rootScope.$emit('unauthorized');
                }
                if (parseInt(rejection.status) === 500) {
                    errorNotify('Server Error. Please try again later.');
                }
                return $q.reject(rejection);
            }
}