From 698ab5b2f794768a3d9ccdbe84a5d3fce24a01fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Tainon?= Date: Wed, 24 Apr 2024 18:19:55 +0200 Subject: [PATCH] When we try to get blocks from xml headless (without the DOM loaded), it should not fail the check --- frontend/stepper/js/index.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/frontend/stepper/js/index.ts b/frontend/stepper/js/index.ts index 54b00efec..94ab2740f 100644 --- a/frontend/stepper/js/index.ts +++ b/frontend/stepper/js/index.ts @@ -189,7 +189,14 @@ export const checkBlocklyCode = function (answer: Document, context: QuickAlgoLi return; } - const blocks = getBlocksFromXml(blockly); + let blocks; + try { + // This method can fail if Blockly is not loaded in the DOM. In this case it's ok we don't make the check + blocks = getBlocksFromXml(blockly); + } catch (e) { + console.error(e); + return; + } const maxInstructions = context.infos.maxInstructions ? context.infos.maxInstructions : Infinity; const totalCount = blocklyCount(blocks, context); @@ -274,7 +281,18 @@ export const getBlocklyBlocksUsage = function (answer: Document, context: QuickA log.getLogger('blockly_runner').debug('blocks usage', answer); - const blocks = getBlocksFromXml(blockly); + let blocks; + try { + // This method can fail if Blockly is not loaded in the DOM. In this case it's ok we don't make the check + blocks = getBlocksFromXml(blockly); + } catch (e) { + console.error(e); + return { + blocksCurrent: 0, + limitations: [], + }; + } + const blocksUsed = blocklyCount(blocks, context); const limitations = (context.infos.limitedUses ? blocklyFindLimited(blocks, context.infos.limitedUses, context) : []) as {type: string, name: string, current: number, limit: number}[];