Skip to content

Commit

Permalink
Add error handling for resource upload errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellrgn committed May 14, 2024
1 parent d75f0d5 commit 461fe6e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/lib/AddFile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
patientName = patient.name[0]?.given[0];
}
if (patientName) {
console.log("Patient name: " + patientName);
label = (patientName !== undefined ? patientName.charAt(0).toUpperCase() + patientName.slice(1).toLowerCase() + "'s" : "My")+ " Summary Link " + new Date().toISOString().slice(0, 10);
}
}
Expand Down Expand Up @@ -301,6 +300,10 @@
status = newStatus;
}
function showError(message: string) {
fetchError = message;
}
function confirmContent() {
submitting = true;
}
Expand Down Expand Up @@ -354,10 +357,11 @@
<ResourceSelector
bind:newResources={resourcesToReview}
bind:patient={patient}
bind:submitSelections={submitting}
bind:submitting={submitting}
bind:injectedResources={resourcesToInject}
on:ips-retrieved={ async ({ detail }) => { uploadRetrievedIPS(detail) } }
on:status-update={ ({ detail }) => { updateStatus(detail) } }
on:error={ ({ detail }) => { showError(detail) } }
/>
{/if}
</Accordion>
Expand Down
21 changes: 15 additions & 6 deletions src/lib/ResourceSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
import OccupationalDataForHealth from './resource-templates/OccupationalDataForHealth.svelte';
export let newResources: Array<any> | undefined;
export let submitSelections: boolean;
export let submitting: boolean;
export let patient: any | undefined;
export let injectedResources: Record<string, {section: any|undefined; resources: { [key: string]: ResourceHelper }}>;
const components: Record<string, any> = {
"DocumentReference": AdvanceDirective,
"Consent": AdvanceDirective,
"AllergyIntolerance": AllergyIntolerance,
"Condition": Condition,
"DiagnosticReport": DiagnosticReport,
Expand All @@ -57,9 +58,9 @@
const ipsDispatch = createEventDispatcher<{ 'ips-retrieved': IPSRetrieveEvent }>();
const statusDispatch = createEventDispatcher<{ 'status-update': string }>();
const errorDispatch = createEventDispatcher<{ 'error': string }>();
let resources:{ [key: string]: ResourceHelper } = {};
let resourcesByType:{ [key: string]: { [key: string]: ResourceHelper} } = {};
let submitting = false;
let reference: string;
let patientReference: string;
let patients: {[key: string]: ResourceHelper} = {};
Expand All @@ -78,8 +79,12 @@
}
}
$: {
if (submitSelections) {
confirm();
if (submitting) {
confirm().catch(error => {
submitting = false;
console.error(error);
errorDispatch("error", error.message);
});
}
}
Expand Down Expand Up @@ -224,15 +229,19 @@
statusDispatch("status-update", "Preparing");
let preparedResources = prepareResources(getSelectedResources());
statusDispatch("status-update", "Adding data");
reference = await uploadResources(preparedResources);
try {
reference = await uploadResources(preparedResources);
} catch (e:any) {
throw new Error("Unable to upload resources", {cause: e});
}
let content:any;
statusDispatch("status-update", "Building IPS");
const contentResponse = await fetch(reference!, {
headers: { accept: 'application/fhir+json' }
}).then(function(response) {
if (!response.ok) {
// make the promise be rejected if we didn't get a 2xx response
// reject the promise if we didn't get a 2xx response
throw new Error("Unable to fetch IPS", {cause: response});
} else {
return response;
Expand Down
7 changes: 5 additions & 2 deletions src/lib/resourceUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const allowableResourceTypes = [
'Consent',
'Condition',
'Immunization',
// 'Procedure', Removed until hapi server thymeleaf fixes are available
'Procedure', // Removed until hapi server thymeleaf fixes are available
'Observation',
'DiagnosticReport',
'MedicationRequest',
Expand Down Expand Up @@ -71,7 +71,10 @@ export async function uploadResources(resources) {
},
body: JSON.stringify(bundle),
}).then((response) => {
return response.json()
if (!response.ok) {
throw new Error('Error uploading resources', { cause: response });
}
return response.json();
}).then((body) => {
let ipsUrl = "";
body.entry.forEach(entry => {
Expand Down

0 comments on commit 461fe6e

Please sign in to comment.