I have a code with AngularJS:
service.doSomething()
.then(function(result) {
//do something with the result
});
In AngularJS 1.5.
First option is simply to hide an error with disablinconfiguring errorOnUnhandledRejections
in $qProvider configuratio as suggested Cengkuru Michael:
app.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}]);
BUT this will only switch off logging. The error itself will remain
The better solution in this case will be - handling a rejection with .catch()
method:
service.doSomething()
.then(function (response) {})
.catch(function (err) {});
Useful Links:
errorOnUnhandledRejections(false); was not a resolution for me.
You do indeed need to define an exception handler... however... wrap it in a timeout function: this will force the original exception/stack trace to be thrown.
To make the error show up as an error in the web console, as you originally intended:
ng.factory('$exceptionHandler', function($log) {
return function(exception, cause) {
// do some some stuff...
setTimeout(function(){
// throw the original exception (with correct line #'s etc)
throw exception;
})
};
});
Heres the timeout trick: Why can I not throw inside a Promise.catch handler?
This has been fixed with fix($q): Add traceback to unhandled promise rejections -- Commit 316f60f and the fix is included in the v1.6.1 release.