This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 862
Expand file tree
/
Copy pathcustom-exception-handler.ts
More file actions
46 lines (39 loc) · 1.63 KB
/
custom-exception-handler.ts
File metadata and controls
46 lines (39 loc) · 1.63 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
// #docregion
import { ExceptionHandler, Injectable } from '@angular/core';
import { ErrorLoggingService } from './error-logging.service';
// #docregion class
@Injectable()
export class CustomExceptionHandler {
constructor(private errorLogggingService: ErrorLoggingService) {}
call(exception: any, stackTrace?: any, reason?: string): void {
this.logToConsole(exception, stackTrace, reason);
try {
// Internally, Angular wraps Error objects inside a proxy; therefore, when we
// send the error to the logging service, we want to try and unwrap the error
// first so that we're more likely to send-over a native Error object.
// --
// CAUTION: This is NOT A DOCUMENTED BEHAVIOR - you have to look at the Angular
// source code in order to see that this happens.
this.errorLogggingService.log(this.unwrapError(exception));
} catch (loggingError) {
this.logToConsole(loggingError);
}
}
private logToConsole(exception: any, stackTrace?: any, reason?: string): void {
// Even though we are replacing the core _instance_ of the ExceptionHandler, we can
// still leverage the core class' static method for stringification of the error.
let stringified = ExceptionHandler.exceptionToString(exception, stackTrace, reason);
if (console && console.group && console.log) {
console.group('Custom Exception Handler');
console.log(stringified);
console.groupEnd();
}
}
private unwrapError(exception: any): any {
while (exception.originalException) {
exception = exception.originalException;
}
return exception;
}
}
// #enddocregion class