forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.ts
More file actions
80 lines (73 loc) · 2.06 KB
/
error_handler.ts
File metadata and controls
80 lines (73 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {inject, InjectionToken} from './di';
import {getOriginalError} from './util/errors';
import {NgZone} from './zone';
/**
* Provides a hook for centralized exception handling.
*
* The default implementation of `ErrorHandler` prints error messages to the `console`. To
* intercept error handling, write a custom exception handler that replaces this default as
* appropriate for your app.
*
* @usageNotes
* ### Example
*
* ```
* class MyErrorHandler implements ErrorHandler {
* handleError(error) {
* // do something with the exception
* }
* }
*
* @NgModule({
* providers: [{provide: ErrorHandler, useClass: MyErrorHandler}]
* })
* class MyModule {}
* ```
*
* @publicApi
*/
export class ErrorHandler {
/**
* @internal
*/
_console: Console = console;
handleError(error: any): void {
const originalError = this._findOriginalError(error);
this._console.error('ERROR', error);
if (originalError) {
this._console.error('ORIGINAL ERROR', originalError);
}
}
/** @internal */
_findOriginalError(error: any): Error | null {
let e = error && getOriginalError(error);
while (e && getOriginalError(e)) {
e = getOriginalError(e);
}
return e || null;
}
}
/**
* `InjectionToken` used to configure how to call the `ErrorHandler`.
*
* `NgZone` is provided by default today so the default (and only) implementation for this
* is calling `ErrorHandler.handleError` outside of the Angular zone.
*/
export const INTERNAL_APPLICATION_ERROR_HANDLER = new InjectionToken<(e: any) => void>(
typeof ngDevMode === 'undefined' || ngDevMode ? 'internal error handler' : '',
{
providedIn: 'root',
factory: () => {
const zone = inject(NgZone);
const userErrorHandler = inject(ErrorHandler);
return (e: unknown) => zone.runOutsideAngular(() => userErrorHandler.handleError(e));
},
},
);