Skip to content

Commit

Permalink
better error messages when dynamic client registration fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed Nov 5, 2023
1 parent 11386db commit bba1dde
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion backend/pkg/models/source_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] and we'll modify this provider to use our Legacy integration: %d", registrationResponse.StatusCode)

}

Expand Down
25 changes: 15 additions & 10 deletions backend/pkg/web/handler/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
}


/**
Expand Down

0 comments on commit bba1dde

Please sign in to comment.