Skip to content

Commit

Permalink
🪲 Remove a source of client exceptions (#5812)
Browse files Browse the repository at this point in the history
A lot of client exceptions were originating from the embedded editor, trying to call `removeAttribute()` on an object that happened to be `null`.

The reason was an assertion that the result of `getElementById()` would never be `null`, where in fact, it was.

Replace with a null coalescing function call `?.removeAttribute()` so that the function call falls to the floor if the element doesn't exist.

**How to test**

Visit http://localhost:8080/embedded/1/?lang=nl&run=true&readOnly=false&fullWidth=false&program=IyBTY2hyaWpmIGpvdXcgY29kZSBoaWVy%22 and run any program. Observe that no exceptions get logged to the JavaScript console.

(cc @jpelay, I spied you [here](#5673) 😛 this is why `!` is dangerous, even you *think* this can never happen there's always a case you didn't think of -- better to program defensively or fail real hard with an explicit error message)
  • Loading branch information
rix0rrr authored Sep 26, 2024
1 parent 2922261 commit 3e184a4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
6 changes: 4 additions & 2 deletions static/js/appbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -104201,14 +104201,15 @@ def note_with_error(value, err):
}
}
function stopDebug() {
var _a3;
if (step_debugger === true) {
var debugButton = $("#debug_button");
debugButton.show();
var continueButton = $("#debug_continue");
var stopButton = $("#debug_stop");
var resetButton = $("#debug_restart");
var runButtonContainer = $("#run_button_container");
document.getElementById("debug_continue").removeAttribute("disabled");
(_a3 = document.getElementById("debug_continue")) == null ? void 0 : _a3.removeAttribute("disabled");
$("#stopit").hide();
$("#runit").show();
runButtonContainer.show();
Expand All @@ -104231,7 +104232,8 @@ def note_with_error(value, err):
}
}
function incrementDebugLine() {
document.getElementById("debug_continue").removeAttribute("disabled");
var _a3;
(_a3 = document.getElementById("debug_continue")) == null ? void 0 : _a3.removeAttribute("disabled");
const active_suspension = theGlobalDebugger.getActiveSuspension();
const suspension_info = theGlobalDebugger.getSuspensionInfo(active_suspension);
const lineNumber = suspension_info.lineno;
Expand Down
4 changes: 2 additions & 2 deletions static/js/appbundle.js.map

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions static/js/debugging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function toggleVariableView(){
if($('#variables #variable_list li').length == 0){
$('#variable_button').hide();
}
else{
else{
$('#variable_button').show();
$('#variable_list').show();
document.getElementById('variables_arrow')!.classList.remove('fa-angle-up');
Expand Down Expand Up @@ -171,7 +171,7 @@ export function initializeDebugger(options: InitializeDebuggerOptions) {

if(options.level != 0){
let level = options.level;
variable_view = level >= 2;
variable_view = level >= 2;
}
initializeBreakpoints(options.editor);
}
Expand Down Expand Up @@ -270,7 +270,7 @@ export function stopDebug() {
var stopButton = $('#debug_stop');
var resetButton = $('#debug_restart');
var runButtonContainer = $('#run_button_container');
document.getElementById('debug_continue')!.removeAttribute('disabled');
document.getElementById('debug_continue')?.removeAttribute('disabled');
$('#stopit').hide();
$('#runit').show()
runButtonContainer.show();
Expand Down Expand Up @@ -298,7 +298,7 @@ function clearDebugVariables() {
}

export function incrementDebugLine() {
document.getElementById('debug_continue')!.removeAttribute('disabled');
document.getElementById('debug_continue')?.removeAttribute('disabled');
const active_suspension = theGlobalDebugger.getActiveSuspension();
const suspension_info = theGlobalDebugger.getSuspensionInfo(active_suspension);
const lineNumber = suspension_info.lineno;
Expand All @@ -321,17 +321,17 @@ export function incrementDebugLine() {
const startingLine = map.python_range.from_line + theGlobalDebugger.get_code_starting_line();
const finishingLine = map.python_range.to_line + theGlobalDebugger.get_code_starting_line();
// Maybe we hit the correct mapping for this line
if (lineNumber >= startingLine && lineNumber <= finishingLine) {
if (lineNumber >= startingLine && lineNumber <= finishingLine) {
// Highlight whole line if it's a full command
if(fullLineCommands.includes(map.command)){
// lines in ace start at 0
if(fullLineCommands.includes(map.command)){
// lines in ace start at 0
const lines = theGlobalEditor.contents.split('\n');
const line = lines[map.hedy_range.from_line - 1];
const ifMatches = ifRe.exec(line);
const repeatMatches = repeatRe.exec(line);
const elseMatches = elseRe.exec(line);
if (ifMatches || repeatMatches || elseMatches) {
theGlobalEditor.setDebuggerCurrentLine(map.hedy_range.from_line,
theGlobalEditor.setDebuggerCurrentLine(map.hedy_range.from_line,
map.hedy_range.from_column, map.hedy_range.to_column - 1);
} else {
theGlobalEditor.setDebuggerCurrentLine(map.hedy_range.from_line);
Expand All @@ -345,20 +345,20 @@ export function incrementDebugLine() {
} else {
const fullLine = lines[map.hedy_range.from_line - 1];
line = fullLine.substring(map.hedy_range.from_column - 1, map.hedy_range.to_column - 1);
}
}
const activeLine: string = theGlobalDebugger.get_source_line(lineNumber - 1);

if (activeLine.match(/ *if/)) {
const ifMatches = ifRe.exec(line);
if (ifMatches) {
const length = ifMatches[1].length;
theGlobalEditor.setDebuggerCurrentLine(map.hedy_range.from_line, map.hedy_range.from_column, map.hedy_range.from_column + length - 1);
theGlobalEditor.setDebuggerCurrentLine(map.hedy_range.from_line, map.hedy_range.from_column, map.hedy_range.from_column + length - 1);
break
}
} else if (activeLine.match(/ *for/)) {
const repeatMatches = repeatRe.exec(line);
if (repeatMatches){
const length = repeatMatches[1].length;
if (repeatMatches){
const length = repeatMatches[1].length;
theGlobalEditor.setDebuggerCurrentLine(map.hedy_range.from_line, map.hedy_range.from_column, map.hedy_range.from_column + length - 1);
break
}
Expand Down

0 comments on commit 3e184a4

Please sign in to comment.