From 0428766ea66510092c6a90c5a00b3a800d39ab41 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Mon, 16 Jan 2023 21:40:32 -0800 Subject: [PATCH] throw and display error messages if Oauth token swap fails -- instead of swallowing error. --- .../medical-sources.component.ts | 22 +++++++++++++++++-- .../src/app/services/lighthouse.service.ts | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/pages/medical-sources/medical-sources.component.ts b/frontend/src/app/pages/medical-sources/medical-sources.component.ts index 508c3bdc..540ceae8 100644 --- a/frontend/src/app/pages/medical-sources/medical-sources.component.ts +++ b/frontend/src/app/pages/medical-sources/medical-sources.component.ts @@ -200,8 +200,9 @@ export class MedicalSourcesComponent implements OnInit { if(callbackError && !callbackCode){ //TOOD: print this message in the UI - console.error("an error occurred while authenticating to this source. Please try again later", callbackErrorDescription) - return + let errMsg = "an error occurred while authenticating to this source. Please try again later" + console.error(errMsg, callbackErrorDescription) + throw new Error(errMsg) } console.log("callback code:", callbackCode) @@ -210,6 +211,12 @@ export class MedicalSourcesComponent implements OnInit { let payload: any payload = await this.lighthouseApi.swapOauthToken(sourceType, sourceMetadata,expectedSourceStateInfo, callbackCode) + if(!payload.access_token || payload.error){ + //if the access token is not set, then something is wrong, + let errMsg = payload.error || "unable to retrieve access_token" + console.error(errMsg) + throw new Error(errMsg) + } //If payload.patient is not set, make sure we extract the patient ID from the id_token or make an introspection req if(!payload.patient && payload.id_token){ @@ -290,6 +297,17 @@ export class MedicalSourcesComponent implements OnInit { console.error(err) }); }) + .catch((err) => { + delete this.status[sourceType] + // window.location.reload(); + + const toastNotification = new ToastNotification() + toastNotification.type = ToastType.Error + toastNotification.message = `An error occurred while accessing ${sourceType}: ${err}` + toastNotification.autohide = false + this.toastService.show(toastNotification) + console.error(err) + }) } diff --git a/frontend/src/app/services/lighthouse.service.ts b/frontend/src/app/services/lighthouse.service.ts index 4cb6fa36..3ec29d82 100644 --- a/frontend/src/app/services/lighthouse.service.ts +++ b/frontend/src/app/services/lighthouse.service.ts @@ -146,7 +146,7 @@ export class LighthouseService { introspection_endpoint: sourceMetadata.introspection_endpoint, } - console.log("STARTING--- Oauth.validateAuthResponse") + console.log("STARTING--- Oauth.validateAuthResponse", as) const params = Oauth.validateAuthResponse(as, client, new URLSearchParams({"code": code, "state": expectedSourceStateInfo.state}), expectedSourceStateInfo.state) if (Oauth.isOAuth2Error(params)) { console.log('error', params)