Skip to content

Commit

Permalink
feat: Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PintoGideon committed Jun 4, 2024
1 parent 918c7f3 commit cda50e2
Showing 1 changed file with 70 additions and 57 deletions.
127 changes: 70 additions & 57 deletions src/components/NewLibrary/useOperations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ const useOperations = () => {
selectedPaths: any,
folderDownloadStatus: any,
) => {
console.log("SelectedPaths", selectedPaths);

const downloadPromises = selectedPaths.map(async (userSelection: any) => {
const { type, payload } = userSelection;
if (type === "folder") {
Expand All @@ -137,23 +135,29 @@ const useOperations = () => {
if (currentStatus === FolderDownloadTypes.zippingFolder) {
const data = await checkIfFeedExists(payload);

if (!data || !data.feed || !data.createdInstance) {
return; // Skip this iteration
}
console.log("Data", data);

const { feed, createdInstance } = data;
dispatch(
downloadFolderStatus(
if (!data || !data.feed || !data.createdInstance) {
setDownloadErrorState(
payload as FileBrowserFolder,
FolderDownloadTypes.zippingFolder,
),
);
await createPipelineDuringDownload(
payload as FileBrowserFolder,
payload.data.path,
createdInstance,
feed,
);
payload.data.path,
"Error download this folder/file",
);
} else {
const { feed, createdInstance } = data;
dispatch(
downloadFolderStatus(
payload as FileBrowserFolder,
FolderDownloadTypes.zippingFolder,
),
);
await createPipelineDuringDownload(
payload as FileBrowserFolder,
payload.data.path,
createdInstance,
feed,
);
}
}
}
}
Expand Down Expand Up @@ -225,7 +229,11 @@ const useOperations = () => {
path,
"Failed to find dircopy",
);
return undefined;
return {
feed: undefined,
folderName: "",
createdInstance: undefined,
};
}
const client = ChrisAPIClient.getClient();
// Check if the feed already exists with this name (Feed downloads cannot be repetitive)
Expand All @@ -245,9 +253,9 @@ const useOperations = () => {
feed = feeds?.[0];
const pluginInstances = await feed?.getPluginInstances({});
const pluginInstancesItems = pluginInstances?.getItems();
console.log("PluginInstanceItems", pluginInstancesItems);
if (pluginInstancesItems) {
createdInstance =
pluginInstances?.data[pluginInstancesItems.length - 1];
createdInstance = pluginInstancesItems[pluginInstancesItems.length - 1];
}
} else {
createdInstance = await client.createPluginInstance(dircopy.data.id, {
Expand All @@ -271,19 +279,19 @@ const useOperations = () => {
path,
"Failed to create a Feed",
);
} else {
await data?.feed?.put({
name: `Library Download for ${data.folderName}`,
});
}

await data?.feed?.put({
name: `Library Download for ${data.folderName}`,
});

return { feed: data?.feed, createdInstance: data?.createdInstance };
};

const createPipelineDuringDownload = async (
payload: FileBrowserFolder,
path: string,
createdInstance: PluginInstance["data"],
createdInstance: PluginInstance,
feed: Feed,
) => {
const client = ChrisAPIClient.getClient();
Expand All @@ -307,9 +315,11 @@ const useOperations = () => {

const { id } = pipeline.data;

console.log("Created Instance", createdInstance);

//@ts-ignore
const workflow = await client.createWorkflow(id, {
previous_plugin_inst_id: createdInstance.id,
previous_plugin_inst_id: createdInstance.data.id,
});

const pluginInstancesList = await workflow.getPluginInstances({
Expand All @@ -328,7 +338,7 @@ const useOperations = () => {
status = statusReq.data.status;
}

const filePath = `home/${username}/feeds/feed_${feed.data.id}/pl-dircopy_${createdInstance.id}/pl-pfdorun_${zipInstance.data.id}/data`;
const filePath = `home/${username}/feeds/feed_${feed.data.id}/pl-dircopy_${createdInstance.data.id}/pl-pfdorun_${zipInstance.data.id}/data`;
if (status === "finishedSuccessfully") {
const folderList = await client.getFileBrowserFolders({
path: filePath,
Expand Down Expand Up @@ -405,41 +415,44 @@ const useOperations = () => {
", ",
)}`,
);
return;
}
} else {
dispatch(
downloadFolderStatus(
payload as FileBrowserFolder,
FolderDownloadTypes.creatingFeed,
),
);

dispatch(
downloadFolderStatus(
/** Create */
const data = await createFeedDuringDownload(
payload as FileBrowserFolder,
FolderDownloadTypes.creatingFeed,
),
);

/** Create */
const data = await createFeedDuringDownload(
payload as FileBrowserFolder,
path,
);

if (!data || !data.feed || !data.createdInstance) {
return;
}
path,
);

const { feed, createdInstance } = data;
if (!data || !data.feed || !data.createdInstance) {
setDownloadErrorState(
payload as FileBrowserFolder,
path,
"Error download this folder/file",
);
} else {
const { feed, createdInstance } = data;

dispatch(
downloadFolderStatus(
payload as FileBrowserFolder,
FolderDownloadTypes.zippingFolder,
),
);
dispatch(
downloadFolderStatus(
payload as FileBrowserFolder,
FolderDownloadTypes.zippingFolder,
),
);

await createPipelineDuringDownload(
payload as FileBrowserFolder,
path,
createdInstance,
feed,
);
await createPipelineDuringDownload(
payload as FileBrowserFolder,
path,
createdInstance,
feed,
);
}
}
}
});

Expand Down

0 comments on commit cda50e2

Please sign in to comment.