From b587e1169abe4c130aa8b0383cde7bf41b931999 Mon Sep 17 00:00:00 2001 From: Jonathan Lim-Breitbart Date: Wed, 22 Dec 2021 14:14:44 -0800 Subject: [PATCH] feat(Error-Handling): Filter errors from external requests (#439) Don't show toast message when external request results in HttpError. --- src/app/http-error.interceptor.ts | 22 +++++++++++++++++++--- src/messages.xlf | 6 +++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/app/http-error.interceptor.ts b/src/app/http-error.interceptor.ts index d34db162cba..29333e3f1b5 100644 --- a/src/app/http-error.interceptor.ts +++ b/src/app/http-error.interceptor.ts @@ -9,10 +9,11 @@ import { import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { MatSnackBar } from '@angular/material/snack-bar'; +import { ConfigService } from './services/config.service'; @Injectable() export class HttpErrorInterceptor implements HttpInterceptor { - constructor(public snackBar: MatSnackBar) {} + constructor(protected configService: ConfigService, public snackBar: MatSnackBar) {} intercept(request: HttpRequest, next: HttpHandler): Observable> { return next.handle(request).pipe( @@ -29,11 +30,26 @@ export class HttpErrorInterceptor implements HttpInterceptor { } if (err.status >= 500 || err.status === 0) { - this.snackBar.open($localize`An error occurred. Please refresh this page and try again.`); + this.processError(err); } - return throwError(err); }) ); } + + processError(err: HttpErrorResponse): void { + try { + const requestUrl = new URL(err.url); + if (requestUrl.origin === this.configService.getWISEHostname()) { + this.showError(); + } + } catch (e) { + // request was from a relative (or invalid) URL + this.showError(); + } + } + + showError() { + this.snackBar.open($localize`An error occurred. Please refresh this page and try again.`); + } } diff --git a/src/messages.xlf b/src/messages.xlf index 56f49382c69..95938dfb726 100644 --- a/src/messages.xlf +++ b/src/messages.xlf @@ -4331,21 +4331,21 @@ An error occurred: src/app/http-error.interceptor.ts - 22 + 23 Backend returned code , body was: src/app/http-error.interceptor.ts - 27 + 28 An error occurred. Please refresh this page and try again. src/app/http-error.interceptor.ts - 32 + 53