From ecbd172b3ff0238a1310c3d4f5a6f1afe86b5476 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 1 Oct 2021 17:13:03 +0000 Subject: [PATCH] fix(notifications): Fix notifications url request failure handling (backport #310) (#311) * fix(notifications): Fix notifications url request failure handling (#310) * Replace concatMap using in response JSON parsing with a switchMap * Revert back to concatMap and throw an error containing the response status code and text * Clean-up syntax * fixup! Clean-up syntax * Log err.message instead of just the err object * Fix code changed during rebase * Include response body for more detailed error info * Clean up imports * Make error logging more specific (cherry picked from commit 9e86184c11e4a12733215137df357d0faa9b8af5) # Conflicts: # src/app/Shared/Services/NotificationChannel.service.tsx * fix(notifications): Fix notifications url request failure handling (#310) * Replace concatMap using in response JSON parsing with a switchMap * Revert back to concatMap and throw an error containing the response status code and text * Clean-up syntax * fixup! Clean-up syntax * Log err.message instead of just the err object * Fix code changed during rebase * Include response body for more detailed error info * Clean up imports * Make error logging more specific * Remove unused imports Co-authored-by: Hareet Dhillon --- .../Services/NotificationChannel.service.tsx | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/app/Shared/Services/NotificationChannel.service.tsx b/src/app/Shared/Services/NotificationChannel.service.tsx index 621bae158..56cfdab72 100644 --- a/src/app/Shared/Services/NotificationChannel.service.tsx +++ b/src/app/Shared/Services/NotificationChannel.service.tsx @@ -36,10 +36,10 @@ * SOFTWARE. */ import { Notifications } from '@app/Notifications/Notifications'; -import { BehaviorSubject, combineLatest, from, Observable, Subject } from 'rxjs'; +import { BehaviorSubject, combineLatest, Observable, Subject } from 'rxjs'; import { fromFetch } from 'rxjs/fetch'; import { webSocket, WebSocketSubject } from 'rxjs/webSocket'; -import { concatMap, filter, first, map } from 'rxjs/operators'; +import { concatMap, filter } from 'rxjs/operators'; import { Base64 } from 'js-base64'; import { ApiService } from './Api.service'; @@ -74,9 +74,17 @@ export class NotificationChannel { const notificationsUrl = fromFetch(`${this.apiSvc.authority}/api/v1/notifications_url`) .pipe( - concatMap(resp => from(resp.json())), - map((url: any): string => url.notificationsUrl) + concatMap(async resp => { + if (resp.ok) { + let body: any = await resp.json(); + return body.notificationsUrl; + } else { + let body: string = await resp.text(); + throw new Error(resp.status + ' ' + body); + } + }) ); + combineLatest(notificationsUrl, this.apiSvc.getToken(), this.apiSvc.getAuthMethod()) .subscribe( (parts: string[]) => { @@ -146,8 +154,8 @@ export class NotificationChannel { } private logError(title: string, err: any): void { - window.console.error(err); - this.notifications.danger(title, JSON.stringify(err)); + window.console.error(err.stack); + this.notifications.danger(title, JSON.stringify(err.message)); } }