fix(healthcare): set responseType to JSON instead of Buffer in importFhirResources.js#4251
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical fix in the healthcare FHIR import sample by ensuring that API responses are correctly parsed as JSON. It also significantly improves the reliability and user feedback for long-running import operations by implementing a more dynamic polling mechanism and comprehensive error handling, making the sample more robust and user-friendly. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes an issue with importing FHIR resources by setting the responseType to json and replacing a fixed-delay wait with a proper polling mechanism for the long-running operation. The changes are a significant improvement. I've added a couple of suggestions to further enhance the robustness of the error handling and the polling logic.
| let done = false; | ||
| let operationStatus; | ||
|
|
||
| // Wait twenty seconds for the LRO to finish. | ||
| await sleep(20000); | ||
| while (!done) { | ||
| console.log('Waiting for import operation to complete...'); | ||
| await sleep(5000); // Wait 5 seconds between polls | ||
|
|
||
| // Check the LRO's status | ||
| const operationStatus = | ||
| await healthcare.projects.locations.datasets.operations.get( | ||
| operationRequest | ||
| ); | ||
| operationStatus = | ||
| await healthcare.projects.locations.datasets.operations.get({ | ||
| name: operationName, | ||
| }); | ||
|
|
||
| const success = operationStatus.data.metadata.counter.success; | ||
| done = operationStatus.data.done; | ||
| } | ||
|
|
||
| if (typeof success !== 'undefined') { | ||
| console.log( | ||
| `Import FHIR resources succeeded. ${success} resources imported.` | ||
| ); | ||
| } else { | ||
| console.log( | ||
| 'Imported FHIR resources failed. Details available in Cloud Logging at the following URL:\n', | ||
| operationStatus.data.metadata.logsUrl | ||
| ); | ||
| if (operationStatus.data.error) { | ||
| console.error( | ||
| 'Import FHIR resources failed:', | ||
| operationStatus.data.error | ||
| ); | ||
| } else { | ||
| const successCount = operationStatus.data.metadata.counter.success || 0; | ||
| console.log( | ||
| `Import FHIR resources succeeded. ${successCount} resources imported.` | ||
| ); | ||
| } |
There was a problem hiding this comment.
The polling loop for the long-running operation could potentially run indefinitely if the operation never reaches a 'done' state. To improve robustness, it's good practice to add a timeout mechanism, such as a maximum number of polling attempts.
// --- POLLING STRATEGY ---
let done = false;
let operationStatus;
const maxAttempts = 24; // Poll for 2 minutes (24 * 5s)
let attempt = 0;
while (!done && attempt < maxAttempts) {
console.log('Waiting for import operation to complete...');
await sleep(5000); // Wait 5 seconds between polls
attempt++;
operationStatus =
await healthcare.projects.locations.datasets.operations.get({
name: operationName,
});
done = operationStatus.data.done;
}
if (!done) {
console.error('The import operation timed out.');
return;
}
if (operationStatus.data.error) {
console.error(
'Import FHIR resources failed:',
operationStatus.data.error
);
} else {
const successCount = operationStatus.data.metadata.counter.success || 0;
console.log(
`Import FHIR resources succeeded. ${successCount} resources imported.`
);
}| ); | ||
| } | ||
| } catch (error) { | ||
| console.error('Error initiating import:', error.message || error); |
There was a problem hiding this comment.
The current error message "Error initiating import:" is misleading. If an error occurs while polling for the operation's status, this message would be incorrect. A more generic error message would be more appropriate to cover all potential failure points within the try block.
console.error('An error occurred during the import process:', error.message || error);
Description
Fixes #
Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.
Checklist
npm test(see Testing)npm run lint(see Style)GoogleCloudPlatform/nodejs-docs-samples. Not a fork.