From bba1dde60683fa18e5ef9c399c385d21033c38dc Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 5 Nov 2023 10:53:30 -0800 Subject: [PATCH] better error messages when dynamic client registration fails. --- backend/pkg/models/source_credential.go | 2 +- backend/pkg/web/handler/source.go | 25 +++++++++++-------- .../medical-sources-connected.component.ts | 11 +++++++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/backend/pkg/models/source_credential.go b/backend/pkg/models/source_credential.go index f8f08bc6..9b0b3341 100644 --- a/backend/pkg/models/source_credential.go +++ b/backend/pkg/models/source_credential.go @@ -199,7 +199,7 @@ func (s *SourceCredential) RegisterDynamicClient() error { if err == nil { log.Printf("Error Response body: %s", string(b)) } - return fmt.Errorf("an error occurred while reading dynamic client registration response, status code was not 200: %d", registrationResponse.StatusCode) + return fmt.Errorf("this institution may not support dynamic client registration, meaning that we cannot automatically fetch your records. Please contact support@fastenhealth.com and we'll modify this provider to use our Legacy integration: %d", registrationResponse.StatusCode) } diff --git a/backend/pkg/web/handler/source.go b/backend/pkg/web/handler/source.go index 96a8ad5a..414d3454 100644 --- a/backend/pkg/web/handler/source.go +++ b/backend/pkg/web/handler/source.go @@ -32,22 +32,25 @@ func CreateReconnectSource(c *gin.Context) { logger.Warnf("This client requires a dynamic client registration, starting registration process") if len(sourceCred.RegistrationEndpoint) == 0 { - logger.Errorln("Empty registration endpoint, cannot be used with dynamic-client registration mode:", sourceCred.DynamicClientRegistrationMode) - c.JSON(http.StatusBadRequest, gin.H{"success": false}) + err := fmt.Errorf("this client requires dynamic registration, but does not provide a registration endpoint: %s", sourceCred.DynamicClientRegistrationMode) + logger.Errorln(err) + c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": err.Error()}) return } err := sourceCred.RegisterDynamicClient() if err != nil { - logger.Errorln("An error occurred while registering dynamic client", err) - c.JSON(http.StatusBadRequest, gin.H{"success": false}) + err = fmt.Errorf("an error occurred while registering dynamic client: %w", err) + logger.Errorln(err) + c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": err.Error()}) return } //generate a JWT token and then use it to get an access token for the dynamic client err = sourceCred.RefreshDynamicClientAccessToken() if err != nil { - logger.Errorln("An error occurred while retrieving access token for dynamic client", err) - c.JSON(http.StatusBadRequest, gin.H{"success": false}) + err = fmt.Errorf("an error occurred while retrieving access token for dynamic client: %w", err) + logger.Errorln(err) + c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": err.Error()}) return } } @@ -56,16 +59,18 @@ func CreateReconnectSource(c *gin.Context) { //reconnect err := databaseRepo.UpdateSource(c, &sourceCred) if err != nil { - logger.Errorln("An error occurred while reconnecting source credential", err) - c.JSON(http.StatusInternalServerError, gin.H{"success": false}) + err = fmt.Errorf("an error occurred while reconnecting source credential: %w", err) + logger.Errorln(err) + c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()}) return } } else { //create source for the first time err := databaseRepo.CreateSource(c, &sourceCred) if err != nil { - logger.Errorln("An error occurred while storing source credential", err) - c.JSON(http.StatusInternalServerError, gin.H{"success": false}) + err = fmt.Errorf("an error occurred while storing source credential: %w", err) + logger.Errorln(err) + c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()}) return } } diff --git a/frontend/src/app/components/medical-sources-connected/medical-sources-connected.component.ts b/frontend/src/app/components/medical-sources-connected/medical-sources-connected.component.ts index 3ba0a0ed..57413d31 100644 --- a/frontend/src/app/components/medical-sources-connected/medical-sources-connected.component.ts +++ b/frontend/src/app/components/medical-sources-connected/medical-sources-connected.component.ts @@ -212,7 +212,7 @@ export class MedicalSourcesConnectedComponent implements OnInit { const toastNotification = new ToastNotification() toastNotification.type = ToastType.Error - toastNotification.message = `An error occurred while accessing ${sourceType}: '${JSON.stringify(err)}'` + toastNotification.message = `An error occurred while accessing ${sourceType}: '${this.extractErrorFromResponse(err)}'` toastNotification.autohide = false toastNotification.link = { text: "View Details", @@ -235,6 +235,15 @@ export class MedicalSourcesConnectedComponent implements OnInit { }) } + extractErrorFromResponse(errResp: any): string { + let errMsg = "" + if(errResp.name == "HttpErrorResponse" && errResp.error && errResp.error?.error){ + errMsg = errResp.error.error + } else { + errMsg = JSON.stringify(errResp) + } + return errMsg + } /**