diff --git a/src/lib/storage/jobs/helpers/performConversion.ts b/src/lib/storage/jobs/helpers/performConversion.ts index 82a724b21..49e74a0d1 100644 --- a/src/lib/storage/jobs/helpers/performConversion.ts +++ b/src/lib/storage/jobs/helpers/performConversion.ts @@ -23,48 +23,55 @@ export default async function performConversion({ req, res, }: ConversionRequest) { - console.log(`Performing conversion for ${id}`); - const storage = new StorageHandler(); - const job = new ConversionJob(DB); - await job.load(id, owner, title); - if (!job.canStart()) { - console.log(`job ${id} was not started`); - return res - ? res.status(405).send({ message: 'Job is already active' }) - : null; - } + try { + console.log(`Performing conversion for ${id}`); + const storage = new StorageHandler(); + const job = new ConversionJob(DB); + await job.load(id, owner, title); + if (!job.canStart()) { + console.log(`job ${id} was not started`); + return res + ? res.status(405).send({ message: 'Job is already active' }) + : null; + } - console.log(`job ${id} is not active, starting`); - await job.start(); + console.log(`job ${id} is not active, starting`); + await job.start(); - if (res) { - res.status(200).send(); - } + if (res) { + res.status(200).send(); + } - const { ws, exporter, settings, bl, rules } = await job.createWorkSpace(api); - const decks = await job.createFlashcards(bl, req, id, rules, settings); - if (!decks) { - await job.failed(); - return; + const { ws, exporter, settings, bl, rules } = await job.createWorkSpace( + api + ); + const decks = await job.createFlashcards(bl, req, id, rules, settings); + if (!decks) { + await job.failed(); + return; + } + const { size, key, apkg } = await job.buildingDeck( + bl, + exporter, + decks, + ws, + settings, + storage, + id, + owner + ); + await notifyUserIfNecessary({ + owner, + rules, + db: DB, + size, + key, + id, + apkg, + }); + await job.completed(); + } catch (error) { + res?.status(400).send('conversion failed.'); + console.error(error); } - const { size, key, apkg } = await job.buildingDeck( - bl, - exporter, - decks, - ws, - settings, - storage, - id, - owner - ); - await notifyUserIfNecessary({ - owner, - rules, - db: DB, - size, - key, - id, - apkg, - }); - await job.completed(); }