Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Errors throw from $onChanges result in an array passed to $exceptionHandler #15578

Closed
jbedard opened this issue Jan 4, 2017 · 2 comments · Fixed by #16492, javascript-indonesias/angular.js#71 or angular-indonesia/angular.js#128

Comments

@jbedard
Copy link
Contributor

jbedard commented Jan 4, 2017

Do you want to request a feature or report a bug?
bug?

What is the current behavior?
an error thrown in an $onChanges method results in an array being thrown which will result in $exceptionHandler(arrayOfErrors)

What is the expected behavior?
Error objects are passed to $exceptionHandler

What is the motivation / use case for changing the behavior?
make $exceptionHandler more predictable / consistent / follow documentation

Which versions of Angular, and which browser / OS are affected by this issue? Did this work in previous versions of Angular? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.
1.5+

Other information (e.g. stacktraces, related issues, suggestions how to fix)
Maybe each error in the array is passed to $exceptionHandler separately?
Maybe the array of errors is wrapped in an Error before being thrown?
Or document that $exceptionHandler might be passed an array?

@IgorAufricht
Copy link

The behaviour also seems to be different based on when the error is thrown.

Consider this example:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<body>

<div ng-app="testModule">
  <div ng-controller="TestController">
    <test-component binding="binding"></test-component>
  </div>
</div>
<script>
angular.module('testModule', [])
  .controller('TestController', function ($scope) {
    setTimeout(function() {
      console.log('updating binding');
      $scope.binding = 'value';
      $scope.$digest();
    }, 500);
  })
  .component('testComponent', {
    bindings: {
      binding: '<'
    },
    controller: function () {
      this.$onChanges = function (changesObj) {
        throw new Error('Error message');
      };
    }
  });

</script>
</body>
</html>

If you run the example above, you will get two errors in console. One is thrown on the initialization of the component, while the second one is thrown when the binding gest updated. The first error is an actual Error (which is correct), while the second one is an array (which is not correct).

@gkalpak
Copy link
Member

gkalpak commented Sep 22, 2017

Yeah, tbh it seems reasonable to pass each error to the $exceptionHandler immediately (i.e. the same we do for each watcher in $digest). Not sure why we did it this way 😕

@Narretz Narretz self-assigned this Mar 9, 2018
@Narretz Narretz modified the milestones: Backlog, 1.6.10 Mar 9, 2018
Narretz added a commit to Narretz/angular.js that referenced this issue Mar 16, 2018
This brings it in line with how we throw errors in a digest cycle.

Closes angular#15578
Narretz added a commit to Narretz/angular.js that referenced this issue Mar 20, 2018
This brings it in line with how we throw errors in a digest cycle.

Closes angular#15578
Narretz added a commit that referenced this issue Mar 22, 2018
This brings it in line with how we throw errors in a digest cycle, 
and also avoids throwing an array.

Closes #15578 
Closes #16492
Narretz added a commit that referenced this issue Apr 12, 2018
This brings it in line with how we throw errors in a digest cycle,
and also avoids throwing an array.

Closes #15578
Closes #16492
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.