All files / src/app/shared/components/error error.component.ts

1.42% Statements 1/70
0% Branches 0/19
0% Functions 0/13
1.42% Lines 1/70

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157                                      1x                                                                                                                                                                                                                                                                                  
import {
  Component, ElementRef, Input, OnDestroy, OnInit, ViewChild
} from '@angular/core';
import { Router, RouterState } from '@angular/router';
import {
  interval, Observable, Subscription, take
} from 'rxjs';
import UAParser from 'ua-parser-js';
import { AppError } from '../../../app.interfaces';
import { MainDataService } from '../../services/maindata/maindata.service';
import { BugReportService } from '../../services/bug-report.service';
import { BugReportResult } from '../../interfaces/bug-report.interfaces';
import { FileService } from '../../services/file.service';
 
@Component({
  selector: 'error',
  templateUrl: 'error.component.html',
  styleUrls: ['error.component.css']
})
export class ErrorComponent implements OnInit, OnDestroy {
  @Input() onBeforeClose: (() => void) | null = null;
  @Input() onClose: ((err: AppError) => void) | null = null;
  @Input() closeCaption: string = '';
  @Input() additionalReport: { [key: string]: string } = {};
  @ViewChild('report') reportElem!: ElementRef;
  error: AppError | null = null;
  errorBuffer: AppError[] = [];
  errorDetailsOpen = false;
  allowErrorDetails = true;
  defaultCloseCaption: string | null = null;
  browser: UAParser.IResult | null = null;
  url: string = '';
  restartTimer$: Observable<number> | null = null;
  waitUnitAutomaticRestartSeconds: number = 30;
  sendingResult: BugReportResult | null = null;
  timestamp: number = -1;
  private appErrorSubscription: Subscription | null = null;
  private restartTimerSubscription: Subscription | null = null;
 
  constructor(
    private mainDataService: MainDataService,
    private router: Router,
    private bugReportService: BugReportService
  ) {
  }
 
  ngOnInit(): void {
    this.browser = new UAParser().getResult();
    this.appErrorSubscription = this.mainDataService.appError$
      .subscribe(err => {
        Iif (err.type === 'network_temporally') {
          this.startRestartTimer();
        }
 
        Iif (err.type === 'session') {
          this.allowErrorDetails = false;
        }
 
        Iif (err.type === 'warning') {
          this.allowErrorDetails = false;
        }
 
        Iif (this.error) {
          this.errorBuffer.push(this.error);
        }
        this.error = err;
 
        this.url = window.location.href;
        this.timestamp = Date.now();
 
        this.setDefaultCloseCaption();
      });
  }
 
  private setDefaultCloseCaption(): void {
    Iif (this.error?.type === 'session') {
      this.defaultCloseCaption = 'Neu Anmelden';
      return;
    }
    this.defaultCloseCaption = null;
  }
 
  private startRestartTimer(): void {
    Iif (this.restartTimer$) {
      return;
    }
    this.restartTimer$ = interval(1000)
      .pipe(take(this.waitUnitAutomaticRestartSeconds));
    this.restartTimerSubscription = this.restartTimer$
      .subscribe({
        complete: () => {
          this.mainDataService.reloadPage();
        }
      });
  }
 
  ngOnDestroy(): void {
    this.appErrorSubscription?.unsubscribe();
    Iif (this.restartTimerSubscription) {
      this.restartTimerSubscription.unsubscribe();
    }
  }
 
  toggleErrorDetails(): void {
    this.errorDetailsOpen = !this.errorDetailsOpen;
  }
 
  closeClick() {
    Iif (this.onBeforeClose) {
      this.onBeforeClose();
    }
    Iif (!this.error) {
      return;
    }
    if (this.onClose) {
      this.onClose(this.error);
    } else {
      this.defaultOnClose();
    }
  }
 
  private defaultOnClose(): void {
    Iif (this.error?.type === 'session') {
      this.mainDataService.resetAuthData();
      const state: RouterState = this.router.routerState;
      const { snapshot } = state;
      const snapshotUrl = (snapshot.url === '/r/login/') ? '' : snapshot.url;
      this.router.navigate(['/r/login', snapshotUrl]);
    }
    Iif (this.error?.type === 'fatal') {
      this.mainDataService.reloadPage();
    }
    Iif (this.error?.type === 'network_temporally') {
      this.mainDataService.reloadPage();
    }
    Iif (this.error?.type === 'session') {
      this.mainDataService.reloadPage(true);
    }
  }
 
  submitReport(): void {
    this.bugReportService.publishReportAtGithub(
      `[${window.location.hostname}] ${this.error?.label}`,
      this.reportElem.nativeElement.innerText,
      this.error?.type ?? ''
    )
      .subscribe(result => { this.sendingResult = result; });
  }
 
  downloadReport(): void {
    FileService.saveBlobToFile(
      new Blob([this.reportElem.nativeElement.innerText], { type: 'text/plain' }),
      'bug-report.txt'
    );
  }
}