|
|
@@ -0,0 +1,40 @@
|
|
|
+import { ErrorHandler, Injectable, NgZone } from "@angular/core";
|
|
|
+import { NotificationMessage, NotificationService, NotificationType } from "./notification.service";
|
|
|
+
|
|
|
+@Injectable()
|
|
|
+export class GlobalErrorHandler implements ErrorHandler {
|
|
|
+
|
|
|
+ public constructor(
|
|
|
+ private readonly errorReporting: NotificationService,
|
|
|
+ private readonly ngZone: NgZone
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ public handleError(error: any): void {
|
|
|
+ if ('rejection' in error) {
|
|
|
+ this.handleProperError(error.rejection);
|
|
|
+ } else if (error instanceof Error) {
|
|
|
+ this.handleProperError(error);
|
|
|
+ } else {
|
|
|
+ this.handleErrorLike(error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private handleProperError(error: Error): void {
|
|
|
+ console.error("ERROR:", error);
|
|
|
+ this.ngZone.run(() => {
|
|
|
+ this.errorReporting.reportError(new NotificationMessage(NotificationType.Error, `${error.name}: ${error.message}`, true, 0));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private handleErrorLike(error: unknown): void {
|
|
|
+ let errorType: string = typeof(error);
|
|
|
+ if (errorType === 'object') {
|
|
|
+ errorType = (error as object).constructor.name;
|
|
|
+ }
|
|
|
+ console.error('Unexpected _type_ of error', errorType);
|
|
|
+ console.error('ERROR:', error);
|
|
|
+ this.ngZone.run(() => {
|
|
|
+ this.errorReporting.reportError(new NotificationMessage(NotificationType.Error, `Unknown Error Type: ${error}`, false, 0));
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|