Skip to content

Commit

Permalink
fix(notifications): Fix notifications url request failure handling (#310
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
Hareet Dhillon authored Oct 1, 2021
1 parent 0055f52 commit 9e86184
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/app/Shared/Services/NotificationChannel.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, distinctUntilChanged, filter, map } from 'rxjs/operators';
import { concatMap, distinctUntilChanged, filter } from 'rxjs/operators';
import { Base64 } from 'js-base64';
import * as _ from 'lodash';
import { ApiService } from './Api.service';
Expand Down Expand Up @@ -107,10 +107,18 @@ 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()])

combineLatest(notificationsUrl, this.apiSvc.getToken(), this.apiSvc.getAuthMethod())
.pipe(distinctUntilChanged(_.isEqual))
.subscribe({
next: (parts: string[]) => {
Expand Down Expand Up @@ -184,8 +192,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));
}
}

Expand Down

0 comments on commit 9e86184

Please sign in to comment.