I want to make a more or less generic error handler in my Angular app.
For instance, i get a mail with this link: mysite/messages/151 Clicking this mail: if i am not logged in, my framework returns an 403 (on purpose).
So i created this factory. For now it is only looking for a 403 error and then routing the user to a login page.
app.factory('httpRequestInterceptor', ['$q', '$location', '$rootScope', function($q, $location) {
return {
'responseError': function(rejection) {
if (rejection.status === 403) {
$location.path('/user/login');
return $q.reject(rejection);
}
}
};
}]);
In the app.config i added this line:
app.config(['$httpProvider', function($httpProvider)
{
$httpProvider.interceptors.push('httpRequestInterceptor');
Now, when i get a 403, i go to the right path: /user/login So far, so good.
But... i want to add the $state somehow so i know what the calling url was (messages/151) so i can return to the right place and show the message.
I can not use $state in the factory (error because of some tech stuff i do not understand yet ($http vs $state?), so now i am confused how to get the things done.
So, i want:
1) on 403 error on URL go to login state (works)
2) get login credentials from user (works)
3) goto URL after login is ok (not a clue)
Maybe i should use another approach, but i can not get my head around it. So hopefully someone can help me out?