Skip to content

Commit

Permalink
error handling in downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Feb 16, 2022
1 parent d2ad9f0 commit c842ad5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/forge-download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ const ItemsApi = new ForgeSDK.ItemsApi();
* @param {string} destination
* @param {ForgeSDK.AuthClient} credentials
*/
exports.download = async (projectID: string, itemID: string, fileName: string, destination: string, credentials: ForgeSDK.AuthToken) => {
const storageLocation = await getStorageLocation(projectID, itemID, credentials);
exports.download = async (
projectID: string,
itemID: string,
fileName: string,
destination: string,
credentials: ForgeSDK.AuthToken
): Promise<string> => {
const storageLocation: string = await getStorageLocation(projectID, itemID, credentials);

logger.info(`download: Downloading file "${fileName}" to ${destination}`);

Expand All @@ -33,17 +39,23 @@ exports.download = async (projectID: string, itemID: string, fileName: string, d
})
.catch((err: ForgeSDK.ApiError) => {
logger.error(`download: Error downloading "${fileName}": ${err.statusCode}`, err);
throw new Error(err.statusMessage);
});
};

const getStorageLocation = (projectID: string, itemID: string, credentials: ForgeSDK.AuthToken) => {
return ItemsApi.getItem(projectID, itemID, config.authClient, credentials)
const getStorageLocation = (
projectID: string,
itemID: string,
credentials: ForgeSDK.AuthToken
): Promise<string> => {
return ItemsApi.getItem(projectID, itemID + '1', config.authClient, credentials)
.then((res: ForgeSDK.ApiResponse) => {
let data = res.body.included[0];
let storageID = data.relationships.storage.data.id;
storageID = storageID.substring(storageID.indexOf("/") + 1);
return storageID;
}).catch((err: ForgeSDK.ApiError) => {
logger.error(`download: Unable to get storage location for ${itemID}: ${err.statusCode}`, err);
throw new Error(err.statusMessage);
});
};
4 changes: 3 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ const createServer = () => {
//TODO: check if extension is in the name
const fileName = body.payload.name;
const destination = __dirname + "/file_share" + config.folderIDtoLocal[body.payload.parentFolderUrn];
await downloader.download(config.projectID, itemID, fileName, destination, credentials);
await downloader.download(config.projectID, itemID, fileName, destination, credentials).catch((err: Error) => {
console.log(err);
});
}
});

Expand Down

0 comments on commit c842ad5

Please sign in to comment.