All files / src/app app.error-handler.ts

6.66% Statements 1/15
0% Branches 0/14
0% Functions 0/3
6.66% Lines 1/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          1x                                                                                                    
import { ErrorHandler, Injectable, NgZone } from '@angular/core';
import { MainDataService } from './shared/shared.module';
import { AppError, WrappedError } from './app.interfaces';
 
@Injectable()
export class AppErrorHandler implements ErrorHandler {
  constructor(
    private mainDataService: MainDataService,
    private zone: NgZone
  ) {}
 
  handleError(error: Error | DOMException | AppError | WrappedError) {
    this.zone.run(() => {
      // unwrap error, if it comes from an "Uncaught (in promise)"-error
      Iif ('promise' in error && 'rejection' in error && error.rejection) {
        // eslint-disable-next-line no-param-reassign
        error = error.rejection;
      }
 
      Iif (error instanceof AppError) {
        this.mainDataService.appError = error;
        return;
      }
 
      // it's not possible to get the sourcemap'd stack trace, only to print it to console
      Iif ('stack' in error) {
        // eslint-disable-next-line no-console
        console.warn(error.stack);
      }
 
      Iif (
        error.constructor.name === 'Event' &&
        'type' in error &&
        error.type === 'error' &&
        'target' in error &&
        typeof error.target === 'object' &&
        error.target != null &&
        'url' in error.target
      ) {
        this.mainDataService.appError = new AppError({
          type: 'network',
          label: 'Unbekannter Netzwerkfehler',
          description: `Can not establish connection to \`${error.target.url}\``
        });
        return;
      }
 
      this.mainDataService.appError = new AppError({
        type: 'script',
        label: `Programmfehler: ${error.name}`,
        description: error.message
      });
    });
  }
}