Inside my Angular
application I handle routes/states via ui-router
. If everything works - it is great. But what is a good way to handle errors occurring inside resolve
functions?
My current solution:
I have a to dedicated error
state (similar to the common 404.html
). Which looks like this:
// inside config()
.state('error', {
url: '/error',
controller: 'ErrorCtrl',
templateUrl: 'error.html' // displays an error message
})
If an error occurs inside resolve
I catch it via the broadcasted $stateChangeError
in m run
function:
angular.module('myModule').run(function($state) {
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
event.preventDefault();
$state.go('error');
});
});
This works, but I want to change my error message inside my 'error.html'
dependent on the error. I don't want to pollute the $rootScope
and I want to do it in a ui-router
'esk way.
My current solution uses $stateParams
to the error data to my error
state, but I have to use JSONified query params for this and get a very ugly URL:
// inside config()
.state('error', {
url: '/error?data&status&config', // accept these three params
controller: 'ErrorCtrl',
templateUrl: 'error.html' // displays an error message
})
angular.module('myModule').run(function($state) {
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
event.preventDefault();
$state.go('error', JSON.stringify(error)); // error has data, status and config properties
});
});
Question
Is there a different way how I can pass the error
object to my error
state without uglifying my URL? (Note: My error
object is complex and not just a simple string.)