feat(python): show custom message if syntax error (#53040)

This commit is contained in:
Oliver Eyton-Williams
2024-01-09 04:25:07 +01:00
committed by GitHub
parent 2965341764
commit 82fea35622
3 changed files with 19 additions and 2 deletions
@@ -373,6 +373,7 @@
"running-tests": "// running tests", "running-tests": "// running tests",
"tests-completed": "// tests completed", "tests-completed": "// tests completed",
"console-output": "// console output", "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", "sign-in-save": "Sign in to save your progress",
"download-solution": "Download my solution", "download-solution": "Download my solution",
"download-results": "Download my results", "download-results": "Download my results",
@@ -209,7 +209,7 @@ function* executeTests(testRunner, tests, testTimeout = 5000) {
throw err; throw err;
} }
} catch (err) { } catch (err) {
const { actual, expected } = err; const { actual, expected, syntaxError } = err;
newTest.message = text newTest.message = text
.replace('--fcc-expected--', expected) .replace('--fcc-expected--', expected)
@@ -217,6 +217,9 @@ function* executeTests(testRunner, tests, testTimeout = 5000) {
if (err === 'timeout') { if (err === 'timeout') {
newTest.err = 'Test timed out'; newTest.err = 'Test timed out';
newTest.message = `${newTest.message} (${newTest.err})`; newTest.message = `${newTest.message} (${newTest.err})`;
} else if (syntaxError) {
newTest.err = 'syntax error';
newTest.message = `<p>${i18next.t('learn.syntax-error')}</p>`;
} else { } else {
const { message, stack } = err; const { message, stack } = err;
newTest.err = message + '\n' + stack; newTest.err = message + '\n' + stack;
@@ -133,7 +133,20 @@ input = __inputGen(${JSON.stringify(input ?? [])})
// Evaluates the learner's code so that any variables they define are // Evaluates the learner's code so that any variables they define are
// available to the test. // 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 // TODO: remove the next line, creating __locals, once all the tests access
// variables directly. // variables directly.
runPython('__locals = globals()'); runPython('__locals = globals()');