Skip to content

Commit 80d32c3

Browse files
committed
added throwing error from angular digest cycle
1 parent 9cbd5b8 commit 80d32c3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ digest duration for several selectors and print sorted table starting with the s
7777
* [ng-count-digest-cycles.js](ng-count-digest-cycles.js) - counts number of full digest cycles (from the root scope)
7878
that run when a scope method executes. Useful because sometimes you can get away with just a local digest
7979
cycle, rather than a full update. See [Local Angular scopes](http://glebbahmutov.com/blog/local-angular-scopes/).
80+
* [ng-throw-error.js](ng-throw-error.js) throws an error from the digest cycle; useful for checking
81+
if your [exception handler](http://glebbahmutov.com/blog/catch-all-errors-in-angular-app/) is working.
8082

8183
All snippets, including mine are distributed under MIT license.
8284

ng-throw-error.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// throws an exception from the digest cycle
2+
// useful to test your exception handler service
3+
// http://glebbahmutov.com/blog/catch-all-errors-in-angular-app/
4+
(function throwErrorFromAngular(angular) {
5+
6+
if (typeof angular === 'undefined') {
7+
throw new Error('Cannot find angular on the page');
8+
}
9+
// select any element in the Elements panel that is inside Angular app
10+
/* global $0 */
11+
var injector = angular.element($0).injector();
12+
if (!injector) {
13+
throw new Error('Cannot find injector, please select an element\n' +
14+
'INSIDE an angular app in the Elements panel');
15+
}
16+
injector.get('$rootScope').$apply(function () {
17+
throw new Error('Error from Angular digest cycle');
18+
});
19+
}(window.angular));

0 commit comments

Comments
 (0)