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

docs($exceptionHandler): add a note about cases when exceptions are not delegated... #9318

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Sep 28, 2014

...to the $exceptionHandler

Add a note in $exceptionHandler's documentation about cases when exceptions are not delegated to the
$exceptionHandler, because they are executed outside of the Angular context. Most notable such
cases being the DOM event listeners registered using jqLite's/jQuery's on/bind methods.

Closes #7909

* {@link ng.$http $http} for XHR, {@link ng.$timeout $timeout}/{@link ng.$interval $interval} for
* setTimeout/setInterval). For the rest, you can explicitly ensure that the code is excecuted
* inside of the Angular context, by wrapping it in
* {@link ng.$rootScope.Scope#$apply scope.$apply()}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you could just manually do

try {
  doAThingThatMightThrow();
} catch (e) {
  $exceptionHandler(e);
}

This is a pretty detailed and long-winded explanation, I think we can condense this down a bit so that it's easier for people to read. SGTY?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try/catch does only take care of exception handling (no $digest-ing). It would get even more verbose to mention both options (and the consequences of each) and since I mention functions executing outside of the Angular context, I rather wouldn't propose a solution that does not address this (out-of-context issue), but only exception-handling.

That said, if you think it's better to remove all that "Angular context" stuff and just keep it down to exception handling it's fine by me.
Or maybe all the examples should go away.

No idea what SGTY is though... :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$digest is completely unrelated to $exceptionHandler, so it doesn't make sense to bring it up (except maybe to mention that things which happen during digest will pass caught exceptions to $exceptionHandler).

SGTY is "sound good to you"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp, how about those versions:


Note, that code executed outside of the Angular context does not delegate exceptions to the $exceptionHandler (e.g. functions invoked by third party libraries and callbacks associated with XHR, setTimeout/setInterval and browser DOM events (including callbacks registered using jqLite's/jQuery's on/bind methods)).
For some of those cases, Angular provides native wrappers that take a care of exception handling (e.g. $http, $timeout/$interval). For the rest, you can explicitly delegate exceptions to the $exceptionHandler, e.g.:

try {
    ...
} catch (e) {
    $exceptionHandler(e);
}

Note, that code executed inside of event-listeners (even those registered using jqLite's/jQuery's on/bind methods) does not delegate exceptions to the $exceptionHandler (unless executed during a digest).
If you wish, you can manually delegate exceptions (e.g. try {...} catch(e) {$exceptionHandler(e);}).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp : I pushed a second commit with a shortened message.

I don't feel strongly about this anyway. I submitted a PR because it was asked for, so feel free to just drop it.

…ot delegated to the

$exceptionHandler

Add a note in $exceptionHandler's documentation about cases when exceptions are not delegated to
the $exceptionHandler, because they are executed outside of the Angular context. Most notable such
cases being the DOM event listeners registered using jqLite's/jQuery's on/bind methods.

Closes angular#7909
@gkalpak gkalpak force-pushed the update-docs-for-$exceptionHandler branch from c87f5f3 to f2a5402 Compare October 9, 2014 15:50
@IgorMinar IgorMinar self-assigned this Oct 9, 2014
@IgorMinar IgorMinar closed this in 257a3f3 Oct 9, 2014
@gkalpak gkalpak deleted the update-docs-for-$exceptionHandler branch October 10, 2014 04:16
@@ -27,6 +27,14 @@
* This example will override the normal action of `$exceptionHandler`, to make angular
* exceptions fail hard when they happen, instead of just logging to the console.
*
* <hr />

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add in the detail from @gkalpak that anything executed outside the AJS context will run into this issue...here's my quick edit:

Note, code executed outside of the Angular context, most commonly DOM event listeners (even those registered using jqLite's on/bind methods), will not delegate exceptions to the {@link ng.$exceptionHandler $exceptionHandler} (unless executed during a digest).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exceptions in event handlers not catched by $exceptionHandler
5 participants