Possibly unhandled rejection in Angular 1.6

前端 未结 9 1561
轻奢々
轻奢々 2020-12-06 04:31

I have a code with AngularJS:

service.doSomething()
  .then(function(result) {
      //do something with the result
  });

In AngularJS 1.5.

相关标签:
9条回答
  • 2020-12-06 04:53

    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:

    • Promise
    • Migrating Angular 1.5 to 1.6
    • $http: remove deprecated callback methods: 'success()/error()'
    0 讨论(0)
  • 2020-12-06 04:55

    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?

    0 讨论(0)
  • 2020-12-06 05:03

    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.

    0 讨论(0)
提交回复
热议问题