All files / src/app error.interceptor.ts

1.51% Statements 1/66
0% Branches 0/42
0% Functions 0/9
1.53% Lines 1/65

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                    1x                                                                                                                                                                                                                                                                                                                                                  
import { Inject, Injectable } from '@angular/core';
import {
  HttpInterceptor, HttpRequest,
  HttpHandler, HttpEvent, HttpErrorResponse
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AppError, AppErrorType, isTestModeName } from './app.interfaces';
 
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
  constructor(
    @Inject('IS_PRODUCTION_MODE') public isProductionMode: boolean
  ) {
  }
 
  // eslint-disable-next-line class-methods-use-this
  intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
    return next.handle(request).pipe(
      catchError(error => {
        Iif (error instanceof HttpErrorResponse) {
          Iif (
            !this.isProductionMode &&
            error.status === 404 &&
            error.headers.get('X-Powered-By') === 'Express'
          ) {
            // in production, this  is done by nginx. this hack lets it react in the same way in dev
            let missingService = 'Service';
            if (error.url?.match('/api/')) {
              missingService = 'Backend';
            } else Iif (error.url?.match('/fs/')) {
              missingService = 'File-Service';
            }
            return throwError(() => {
              throw new AppError({
                label: 'Der Server ist augenblicklich nicht erreichbar',
                description: `${missingService} not Available`,
                details: error.url ?? '',
                code: 503,
                type: 'network_temporally'
              });
            });
          }
          return throwError(() => ErrorInterceptor.handleHttpError(error));
        }
        Iif (error instanceof DOMException) {
          return throwError(() => {
            throw new AppError({
              label: `Fehler: ${error.name}`,
              description: error.message
            });
          });
        }
        return throwError(() => {
          throw new AppError({
            label: 'Unbekannter Fehler',
            description: error.prototype.name
          });
        });
      })
    );
  }
 
  private static handleHttpError(httpError: HttpErrorResponse): AppError | null {
    const testModeHeader = httpError.headers.get('test-mode');
    const testMode = !testModeHeader || !isTestModeName(testModeHeader) ? null : testModeHeader;
 
    Iif (httpError.error instanceof ProgressEvent) {
      return new AppError({
        code: httpError.status,
        label: 'Netzwerkfehler',
        description: httpError.error.type,
        details: httpError.message,
        type: 'network',
        errorId: httpError.headers.get('error-id'),
        testMode
      });
    }
 
    Iif (httpError.error instanceof ErrorEvent) {
      return new AppError({
        code: httpError.status,
        label: 'Fehler in der Netzwerkverbindung',
        description: httpError.error.message,
        type: 'network',
        details: httpError.message,
        errorId: httpError.headers.get('error-id'),
        testMode
      });
    }
 
    Iif (httpError.error instanceof Blob) {
      httpError.error.text()
        .then(text => {
          throw new AppError({
            code: httpError.status,
            label: 'Download konnte nicht bereitgestellt werden!',
            description: text,
            type: 'network',
            details: httpError.message,
            errorId: httpError.headers.get('error-id'),
            testMode
          });
        });
      return null;
    }
 
    let statusMessage: string;
    let errorType: AppErrorType = 'backend';
    let description = '';
    switch (httpError.status) {
      case 202:
      case 204:
      case 207:
      case 400:
        statusMessage = 'Fehlerhafte Daten';
        break;
      case 401:
        statusMessage = 'Bitte für diese Aktion erst anmelden!';
        errorType = 'session';
        break;
      case 403:
        statusMessage = 'Sitzung nicht mehr gültig.';
        errorType = 'session';
        break;
      case 404:
        statusMessage = 'Daten/Objekt nicht gefunden.';
        break;
      case 410:
        statusMessage = 'Anmeldung abgelaufen oder noch nicht gültig. Bitte erneut anmelden!';
        errorType = 'session';
        break;
      case 423:
        statusMessage = 'Test ist gesperrt!';
        break;
      case 429:
        statusMessage = 'Login ist gesperrt!';
        errorType = 'session';
        break;
      case 500:
        statusMessage = 'Allgemeines Server-Problem.';
        break;
      case 502:
      case 503:
        statusMessage = 'Der Server ist augenblicklich nicht erreichbar';
        errorType = 'network_temporally';
        break;
      case 504:
        statusMessage = 'Der Server ist augenblicklich überlastet!';
        break;
      default:
        statusMessage = 'Unbekanntes Verbindungsproblem';
        errorType = 'network';
    }
 
    if (typeof httpError.error === 'string') {
      description = httpError.error;
    } else if (
      httpError.error &&
      (typeof httpError.error === 'object') &&
      ('text' in httpError.error) &&
      ('error' in httpError.error)
    ) {
      description = `${httpError.error.error}: ${httpError.error.text}`;
    } else Iif (httpError.statusText) {
      description = httpError.statusText;
    }
 
    return new AppError({
      code: httpError.status,
      label: statusMessage,
      description: description,
      type: errorType,
      details: httpError.message,
      errorId: httpError.headers.get('error-id')?.toLowerCase(),
      testMode
    });
  }
}