Skip to content

Commit

Permalink
Add timeout to $populate operation
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Oct 24, 2023
1 parent cd6326e commit 86b579d
Showing 1 changed file with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,71 @@ export async function requestPopulate(
authToken: fhirClient.state.tokenResponse!.access_token!
};

if (IS_IN_APP_POPULATE) {
return await populate(inputParameters, fetchResourceCallback, requestConfig);
}
const populatePromise = IS_IN_APP_POPULATE
? populate(inputParameters, fetchResourceCallback, requestConfig)
: fhirClient.request({
url: 'Questionnaire/$populate',
method: 'POST',
body: JSON.stringify(inputParameters),
headers: {
...HEADERS,
'Content-Type': 'application/json',
Authorization: `Bearer ${requestConfig.authToken}`
}
});

try {
const promiseResult = await addTimeoutToPromise(populatePromise, 10000);

const outputParameters = await fhirClient.request({
url: 'Questionnaire/$populate',
method: 'POST',
body: JSON.stringify(inputParameters),
headers: {
...HEADERS,
'Content-Type': 'application/json',
Authorization: `Bearer ${requestConfig.authToken}`
if (promiseResult.timeout) {
return {
resourceType: 'OperationOutcome',
issue: [
{
severity: 'error',
code: 'timeout',
details: { text: '$populate operation timed out.' }
}
]
};
}

if (isOutputParameters(promiseResult)) {
return promiseResult;
}
});

if (isOutputParameters(outputParameters)) {
return outputParameters;
return {
resourceType: 'OperationOutcome',
issue: [
{
severity: 'error',
code: 'invalid',
details: { text: 'Output parameters do not match the specification.' }
}
]
};
} catch (error) {
console.error('Error:', error);
return {
resourceType: 'OperationOutcome',
issue: [
{
severity: 'error',
code: 'unknown',
details: { text: 'An unknown error occurred.' }
}
]
};
}
}

return {
resourceType: 'OperationOutcome',
issue: [
{
severity: 'error',
code: 'invalid',
details: { text: 'Output parameters does not match the specification.' }
}
]
};
async function addTimeoutToPromise(promise: Promise<any>, timeoutMs: number) {

Check warning on line 94 in apps/smart-forms-app/src/features/prepopulate/api/requestPopulate.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`Promise timed out after ${timeoutMs} milliseconds`));
}, timeoutMs);
});

// Use Promise.race to wait for either the original promise or the timeout promise
return Promise.race([promise, timeoutPromise]);
}

0 comments on commit 86b579d

Please sign in to comment.