From 1f1b1a9696ccdd2155cece3c5dccdb08c01025fb Mon Sep 17 00:00:00 2001 From: kytwb Date: Fri, 7 Jan 2022 12:50:26 +0100 Subject: Remove try/catch in getRecipePackage --- src/api/server/ServerApi.ts | 84 +++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 45 deletions(-) (limited to 'src/api') diff --git a/src/api/server/ServerApi.ts b/src/api/server/ServerApi.ts index c5c658df4..706520cb3 100644 --- a/src/api/server/ServerApi.ts +++ b/src/api/server/ServerApi.ts @@ -390,60 +390,54 @@ export default class ServerApi { } async getRecipePackage(recipeId: string) { - try { - const recipesDirectory = userDataRecipesPath(); - const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId); - const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz'); - - const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`); - - ensureDirSync(recipeTempDirectory); + const recipesDirectory = userDataRecipesPath(); + const recipeTempDirectory = join(recipesDirectory, 'temp', recipeId); + const tempArchivePath = join(recipeTempDirectory, 'recipe.tar.gz'); - let archivePath: PathOrFileDescriptor; + const internalRecipeFile = asarRecipesPath(`${recipeId}.tar.gz`); - if (pathExistsSync(internalRecipeFile)) { - debug('[ServerApi::getRecipePackage] Using internal recipe file'); - archivePath = internalRecipeFile; - } else { - debug('[ServerApi::getRecipePackage] Downloading recipe from server'); - archivePath = tempArchivePath; + ensureDirSync(recipeTempDirectory); - const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; - - const res = await window.fetch(packageUrl); - debug('Recipe downloaded', recipeId); - const blob = await res.blob(); - const buffer = await blob.arrayBuffer(); - writeFileSync(tempArchivePath, Buffer.from(buffer)); - } - debug(archivePath); + let archivePath: PathOrFileDescriptor; - await sleep(10); + if (pathExistsSync(internalRecipeFile)) { + debug('[ServerApi::getRecipePackage] Using internal recipe file'); + archivePath = internalRecipeFile; + } else { + debug('[ServerApi::getRecipePackage] Downloading recipe from server'); + archivePath = tempArchivePath; - // @ts-expect-error No overload matches this call. - await tar.x({ - file: archivePath, - cwd: recipeTempDirectory, - preservePaths: true, - unlink: true, - preserveOwner: false, - onwarn: x => debug('warn', recipeId, x), - }); + const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; - await sleep(10); + const res = await window.fetch(packageUrl); + debug('Recipe downloaded', recipeId); + const blob = await res.blob(); + const buffer = await blob.arrayBuffer(); + writeFileSync(tempArchivePath, Buffer.from(buffer)); + } + debug(archivePath); + + await sleep(10); + + // @ts-expect-error No overload matches this call. + await tar.x({ + file: archivePath, + cwd: recipeTempDirectory, + preservePaths: true, + unlink: true, + preserveOwner: false, + onwarn: x => debug('warn', recipeId, x), + }); - const { id } = readJsonSync(join(recipeTempDirectory, 'package.json')); - const recipeDirectory = join(recipesDirectory, id); - copySync(recipeTempDirectory, recipeDirectory); - removeSync(recipeTempDirectory); - removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz')); + await sleep(10); - return id; - } catch (error) { - console.error(error); + const { id } = readJsonSync(join(recipeTempDirectory, 'package.json')); + const recipeDirectory = join(recipesDirectory, id); + copySync(recipeTempDirectory, recipeDirectory); + removeSync(recipeTempDirectory); + removeSync(join(recipesDirectory, recipeId, 'recipe.tar.gz')); - return false; - } + return id; } // Health Check -- cgit v1.2.3-54-g00ecf