Facendo seguito all'articolo precedente, oggi vedremo come gestire gli errori di un'applicazione AngularJs. Il nostro web service potrebbe infatti ritornare una serie di errori, in base all'hash della URL fornito dall'utente.

Nella funzione:

myApp.config(function($httpProvider, $routeProvider) {
});
Occorrerà aggiungere il seguente interceptor:
$httpProvider.interceptors.push(function($q, $location) {
    return {
        'responseError': function(rejection) {
            switch (rejection.status) {
            case 403:
                $location.path('/403').replace();
                break;
            case 404:
                $location.path('/404').replace();
                break;
            case 500:
                $location.path('/500').replace();
                break;
        }
        return $q.reject(rejection);
      }
    }
  });
E le relative route:
    $routeProvider
    .when('/403', {
        templateUrl : 'template-403.html'
    })
    .when('/404', {
        templateUrl : 'template-404.html'
    })
    .when('/500', {
        templateUrl : 'template-500.html'
    })
comments powered by Disqus