import { Injectable } from '@angular/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpResponse, HttpErrorResponse, } from '@angular/common/http'; import { Observable, tap } from 'rxjs'; import { MatSnackBar } from '@angular/material/snack-bar'; @Injectable() export class CustomerInfoInterceptor implements HttpInterceptor { constructor(private _snackBar: MatSnackBar) {} intercept( request: HttpRequest, next: HttpHandler ): Observable> { return next.handle(request).pipe( tap( (event) => { if (event instanceof HttpResponse) { const body = event.body as any; const url = new URL(event.url || ''); if (url.pathname.includes('/icard-proxy/customer_info')) { if ( body.customer_info?.errorCode === 'Customer_CustomerNotFound' ) { this._snackBar.open( 'Пользователь не найден в системе! Обратитесь к руководству', 'Ок' ); } else if ('Error' in body) { this._snackBar.open('Произошла ошибка! Попробуйте снова', 'Ок'); } } } if (event instanceof HttpErrorResponse) { this._snackBar.open('Произошла ошибка! Попробуйте снова', 'Ок'); } }, (err) => { console.error('ERROR FROM INTERCEPTOR: ', err); } ) ); } }