From ea4f561bcfb2320d22b008d62fb928008d1778c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20S=C3=A1nchez?= Date: Wed, 26 Aug 2020 07:33:00 +0200 Subject: [PATCH] Added try-catch --- .../item-image-queue/downloadImages.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/modules/item/back/methods/item-image-queue/downloadImages.js b/modules/item/back/methods/item-image-queue/downloadImages.js index bd22bc281..c49cb0702 100644 --- a/modules/item/back/methods/item-image-queue/downloadImages.js +++ b/modules/item/back/methods/item-image-queue/downloadImages.js @@ -19,6 +19,7 @@ module.exports = Self => { Self.downloadImages = async() => { const models = Self.app.models; + let image; try { const tempPath = path.join('/tmp/salix-image'); @@ -26,7 +27,7 @@ module.exports = Self => { await fs.mkdir(tempPath, {recursive: true}); const timer = setInterval(async() => { - const image = await Self.findOne({where: {error: null}}); + image = await Self.findOne({where: {error: null}}); // Exit loop if (!image) return clearInterval(timer); @@ -61,17 +62,25 @@ module.exports = Self => { await errorHandler(image.itemFk, error, filePath); } }); - }, 1000); + }, 1500); } catch (error) { await errorHandler(image.itemFk, error); } async function errorHandler(rowId, error, filePath) { - const row = await Self.findById(rowId); - await row.updateAttribute('error', error); + try { + const row = await Self.findById(rowId); - if (filePath) - await fs.unlink(filePath); + if (!row) + throw new Error(`Could not update due error ${error}`); + + await row.updateAttribute('error', error); + + if (filePath) + await fs.unlink(filePath); + } catch (error) { + throw error; + } } }; };