aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/http-interceptors/http-error.interceptor.ts
blob: 61d55e0a5a088541a11f1e9c18d77e680e101d2b (plain)
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
/*
 * Copyright (c) 2022. Deutsche Telekom AG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AlertService } from '../modules/alerting';
import { TranslateService } from '@ngx-translate/core';
import { Problem } from '../../../openapi/output';
import { Router } from '@angular/router';
import { DEFAULT_TIMEOUT } from './timeout.interceptor';

/**
 * This class adds global handling of http-request related errors
 */

export enum RequestMethod {
  DELETE = 'DELETE',
  POST = 'POST',
  GET = 'GET',
}

interface ProblemDetail {
  errorDetail: Problem;
  requestId?: string;
  urlTree: string[];
}
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
  errorDetail!: Problem;
  constructor(private alertService: AlertService, private translateService: TranslateService, private router: Router) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError(rsp => {
        const urlTree = this.router.url.split('/');
        this.errorDetail = this.createErrorDetail(rsp);
        const requestId = request.headers.get('x-request-id') || undefined;
        const detail: ProblemDetail = {
          errorDetail: this.errorDetail,
          requestId,
          urlTree,
        };

        if (request.url.includes('onap_logging')) {
          this.alertService.warn(this.translateService.instant('common.block.logging'), {
            id: 'onap_logging',
          });
          return throwError(this.errorDetail);
        }
        if (request.method === RequestMethod.POST && request.url.includes('keycloak')) {
          this.alertService.error(this.translateService.instant('common.block.authorization'), {
            id: 'keycloak',
          });
          return throwError(this.errorDetail);
        }
        if (request.url.includes('preferences')) {
          this.getPreferenceMessage(request, detail);
          return throwError(this.errorDetail);
        }
        if (request.url.includes('actions')) {
          this.getActionMessage(request, detail);
          return throwError(this.errorDetail);
        }

        switch (urlTree[1].split('?')[0]) {
          case 'user-administration':
            this.getUserAdministrationMessage(request, detail, urlTree);
            break;
          case 'dashboard':
            this.getErrorMessage('dashboard', detail);
            break;
          case 'app-starter':
            this.getErrorMessage('appStarter', detail);
            break;
          default:
            this.getErrorMessage('defaultMessage', detail);
            break;
        }
        return throwError(this.errorDetail);
      }),
    );
  }

  private getUserAdministrationMessage(request: HttpRequest<any>, detail: ProblemDetail, urlTree: string[]) {
    if (request.method === RequestMethod.DELETE) {
      return this.getErrorMessage('userAdministration.delete', detail);
    }
    if (urlTree.includes('create')) {
      return this.getErrorMessage('userAdministration.create', detail);
    }
    if (urlTree.includes('edit')) {
      return this.getErrorMessage('userAdministration.edit', detail);
    }
  }

  private getActionMessage(request: HttpRequest<any>, detail: ProblemDetail) {
    if (request.method === RequestMethod.POST) {
      return this.getErrorMessage('saveAction', detail);
    }
    this.getErrorMessage('loadAction', detail);
  }

  private getPreferenceMessage(request: HttpRequest<any>, detail: ProblemDetail) {
    if (request.method === RequestMethod.POST) {
      return this.getErrorMessage('savePreferences', detail);
    }
    this.getErrorMessage('loadPreferences', detail);
  }

  private createErrorDetail(response: any): Problem {
    if (response instanceof TimeoutError) {
      return {
        detail: this.translateService.instant('common.block.timeout', { value: DEFAULT_TIMEOUT / 1000 }),
        title: response.name,
        status: 408,
      };
    }
    return response.error ? response.error : response;
  }

  private getErrorMessage(type: string, detail: ProblemDetail) {
    this.alertService.error(`${this.translateService.instant('common.block.' + type)}`, detail);
  }
}