Skip to content

Commit f82eba8

Browse files
committed
docs($exceptionHandler): include better example
The previous example overwrote the `$exceptionHandler` in order to re-throw the error. This is not recommended, since it would leave the application in an inconsistent state and prevent proper clean-up. The new example simulates logging the error both to the backend and the console. Fixes angular#14704
1 parent 96e34e3 commit f82eba8

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/ng/exceptionHandler.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
*
1616
* ## Example:
1717
*
18+
* The example below will overwrite the default `$exceptionHandler` in order to (a) log uncaught
19+
* errors to the backend for later inspection by the developers and (b) to use `$log.warn()` instead
20+
* of `$log.error()`.
21+
*
1822
* ```js
19-
* angular.module('exceptionOverride', []).factory('$exceptionHandler', function() {
20-
* return function(exception, cause) {
21-
* exception.message += ' (caused by "' + cause + '")';
22-
* throw exception;
23-
* };
24-
* });
23+
* angular.
24+
* module('exceptionOverwrite', []).
25+
* factory('$exceptionHandler', ['$log', 'logErrorsToBackend', function($log, logErrorsToBackend) {
26+
* return function myExceptionHandler(exception, cause) {
27+
* logErrorsToBackend(exception, cause);
28+
* $log.warn(exception, cause);
29+
* };
30+
* });
2531
* ```
2632
*
27-
* This example will override the normal action of `$exceptionHandler`, to make angular
28-
* exceptions fail hard when they happen, instead of just logging to the console.
29-
*
3033
* <hr />
3134
* Note, that code executed in event-listeners (even those registered using jqLite's `on`/`bind`
3235
* methods) does not delegate exceptions to the {@link ng.$exceptionHandler $exceptionHandler}
@@ -36,7 +39,7 @@
3639
* `try { ... } catch(e) { $exceptionHandler(e); }`
3740
*
3841
* @param {Error} exception Exception associated with the error.
39-
* @param {string=} cause optional information about the context in which
42+
* @param {string=} cause Optional information about the context in which
4043
* the error was thrown.
4144
*
4245
*/

0 commit comments

Comments
 (0)