From 82fea356228015de681da47143f0feb4842f318e Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 9 Jan 2024 04:25:07 +0100 Subject: [PATCH] feat(python): show custom message if syntax error (#53040) --- client/i18n/locales/english/translations.json | 1 + .../Challenges/redux/execute-challenge-saga.js | 5 ++++- .../browser-scripts/python-test-evaluator.ts | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/client/i18n/locales/english/translations.json b/client/i18n/locales/english/translations.json index 0a565f7e834..80ab3e87a48 100644 --- a/client/i18n/locales/english/translations.json +++ b/client/i18n/locales/english/translations.json @@ -373,6 +373,7 @@ "running-tests": "// running tests", "tests-completed": "// tests completed", "console-output": "// console output", + "syntax-error": "Your code raised a error before we could run any tests. Please fix it and try again.", "sign-in-save": "Sign in to save your progress", "download-solution": "Download my solution", "download-results": "Download my results", diff --git a/client/src/templates/Challenges/redux/execute-challenge-saga.js b/client/src/templates/Challenges/redux/execute-challenge-saga.js index 31f0f8da0a2..d2516f46e5d 100644 --- a/client/src/templates/Challenges/redux/execute-challenge-saga.js +++ b/client/src/templates/Challenges/redux/execute-challenge-saga.js @@ -209,7 +209,7 @@ function* executeTests(testRunner, tests, testTimeout = 5000) { throw err; } } catch (err) { - const { actual, expected } = err; + const { actual, expected, syntaxError } = err; newTest.message = text .replace('--fcc-expected--', expected) @@ -217,6 +217,9 @@ function* executeTests(testRunner, tests, testTimeout = 5000) { if (err === 'timeout') { newTest.err = 'Test timed out'; newTest.message = `${newTest.message} (${newTest.err})`; + } else if (syntaxError) { + newTest.err = 'syntax error'; + newTest.message = `

${i18next.t('learn.syntax-error')}

`; } else { const { message, stack } = err; newTest.err = message + '\n' + stack; diff --git a/tools/client-plugins/browser-scripts/python-test-evaluator.ts b/tools/client-plugins/browser-scripts/python-test-evaluator.ts index 412bd102924..7191c8420fc 100644 --- a/tools/client-plugins/browser-scripts/python-test-evaluator.ts +++ b/tools/client-plugins/browser-scripts/python-test-evaluator.ts @@ -133,7 +133,20 @@ input = __inputGen(${JSON.stringify(input ?? [])}) // Evaluates the learner's code so that any variables they define are // available to the test. - runPython(code); + try { + runPython(code); + } catch (err) { + // We don't, yet, want user code to raise exceptions. When they do, we + // count this as a failing test. + ctx.postMessage({ + err: { + message: (err as Error).message, + stack: (err as Error).stack, + syntaxError: true + } + }); + return; + } // TODO: remove the next line, creating __locals, once all the tests access // variables directly. runPython('__locals = globals()');