bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Possibly unhandled rejection: cancel error when closing modal

Open eassa opened this issue 8 years ago • 20 comments

after upgrading to angularjs 1.6.3 every time i close modal using : "$uibModalInstance.dismiss('cancel');" i get console error :

Possibly unhandled rejection: cancel

the same when i close the modal clicking outside of it , it tells me

Possibly unhandled rejection: backdrop click

eassa avatar Mar 28 '17 13:03 eassa

+1

branimir93 avatar Mar 31 '17 12:03 branimir93

The problem is that, as the doc says: result (Type: promise) - Is resolved when a modal is closed and rejected when a modal is dismissed. I think dialog dismiss should not be handled with promise rejection as this is not an error.

mdarveau avatar Apr 03 '17 02:04 mdarveau

I'm also facing the similar issue like eassa, is there any solution for this?

xts-velkumars avatar Apr 05 '17 09:04 xts-velkumars

+1

jasonvoirin avatar Apr 05 '17 16:04 jasonvoirin

The way I handle it is:

$uibModal.open({
 ...
}).result.catch(function(res) {
  if (!(res === 'cancel' || res === 'escape key press')) {
    throw res;
  }
});

This basically swallow the cancelled or escape key rejection.

mdarveau avatar Apr 06 '17 04:04 mdarveau

Here was my solution.... uibModalInstance.result.catch(function () { uibModalInstance.close(); })

jasonvoirin avatar Apr 06 '17 17:04 jasonvoirin

Make sure you have the second function argument (cancel one) to avoid the warning message.

modalInstance.result.then(function() {
  // Success	
}, function() {
  // Cancel
});

roger2hk avatar Apr 10 '17 08:04 roger2hk

For me I solved it using: this.$uibModalInstance.close(false); instead of this.$uibModalInstance.dismiss("cancel");

greatsayan avatar Jun 20 '17 14:06 greatsayan

@greatsayan I believe you'd still get unhandled rejection upon clicking outside the modal to dismiss.

raxityo avatar Jun 20 '17 16:06 raxityo

@raxityo No because I disabled this feature! the user cannot close the modal by clicking outside of it

greatsayan avatar Jun 20 '17 19:06 greatsayan

@greatsayan thanks!

JavyMB avatar Jul 05 '17 14:07 JavyMB

Getting bitten by this on AngularJS 1.6.4 with UI-Bootstrap 2.5.0 I use @jasonvoirin's solution.

AnalyzePlatypus avatar Jul 09 '17 20:07 AnalyzePlatypus

as a quick fix the following worked for me:

angular
    .module('app')
    .config(['$provide', function ($provide) {
        $provide.decorator("$exceptionHandler", ['$delegate', '$injector', function ($delegate, $injector) {
            return function (exception, cause) {
                var exceptionsToIgnore = ['Possibly unhandled rejection: backdrop click', 'Possibly unhandled rejection: cancel', 'Possibly unhandled rejection: escape key press']
                if (exceptionsToIgnore.indexOf(exception) >= 0) {
                    return;
                }
                $delegate(exception, cause);                    
            };
        }]);
    }]);

AlexKichkailo avatar Jul 13 '17 13:07 AlexKichkailo

This works well:

$uibModal.open({
    ...
}).result.catch(function (resp) {
    if (['cancel', 'backdrop click', 'escape key press'].indexOf(resp) === -1) throw resp;
});

JewelsJLF avatar Sep 20 '17 16:09 JewelsJLF

The answers above work only for single rejection. But what is if there are several hundreds ones in the project? The simple answer is to set errorOnUnhandledRejections to false in config (by default it's true), such as: qProvider.errorOnUnhandledRejections(false); URL: https://docs.angularjs.org/api/ng/provider/$qProvider

ArtemBahmat avatar Sep 22 '17 08:09 ArtemBahmat

@ArtemBahmat, I would not recommend setting errorOnUnhandledRejections to false. You will end up setting it to true again because of swallowed errors in promises. Unless you're sure that all rejections in your application are properly handled.

A better way is to implement a global $exceptionHandler where you can decide whether you want to log exception and raise an error or skip the rejection.

AlexKichkailo avatar Sep 22 '17 10:09 AlexKichkailo

@JewelsJLF Thanks . It works Well 👍

adityaadhikari15 avatar Oct 11 '17 09:10 adityaadhikari15

With AngularJs v1.6.6, I just use:

$uibModalInstance.close();

This worked for me.

omostan avatar Nov 17 '17 16:11 omostan

For a background on what causes these errors, why you want to avoid setting errorOnUnhandledRejections to false and how to properly handle them see this link: http://www.codelord.net/2017/08/20/angular-1-dot-6-s-possibly-unhandled-rejection-errors/

ZackKnopp avatar Jan 25 '18 15:01 ZackKnopp

I will just add what worked for me many times when using version 2.01: The error can be avoided by using this syntax for handling the promise rejection by $uibModalInstance.dismiss():

     // Use this style to avoid the warning :) 
      var modalInstance = $uibModal.open(…);
      modalInstance.result.then(function(result){
        // use promise.resolve(result) from .close()
      }, function(result){
        // handle promise.reject(), or leave method empty to do nothing, whatever!
      });

The more verbose syntax will throw the warning – however the .then & .catch methods will still work:

     // This style will result in warning :/
      var modalInstance = $uibModal.open(…);
      modalInstance.result.then(function(result){
        // use promise.resolve(result) from .close()
      });
      modalInstance.result.catch(function(result){
        // This will still work, but you will get an error
        // handle promise.reject(), or leave method empty to do nothing, whatever!
      });

auginator avatar Jun 27 '18 07:06 auginator