You can use an interceptor and do whatever you want when an error occurs:
var app = angular.module("myApp", []);
app.config(function ($provide, $httpProvider) {
$provide.factory('ErrorInterceptor', function ($q) {
return {
responseError: function(rejection) {
console.log(rejection);
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('ErrorInterceptor');
});
With this interceptor, you can read the status code and do what you need (the ideal use case is to redirect the user to the login page if the status code is 401).
Since ngResource uses $ http, your hooks will also execute when the resource method is called.
Of course, you can do more and add an interceptor before / after the request.
See the full documentation here: http://docs.angularjs.org/api/ng.$http
See this script: http://jsfiddle.net/4Buyn/ for a working sample.