Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 4c7f27a

Browse files
committed
Add error message implementation!
1 parent 421853f commit 4c7f27a

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

runestone/activecode/js/activecode.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,70 @@ ActiveCode.prototype.toggleEditorVisibility = function () {
459459

460460
ActiveCode.prototype.addErrorMessage = function (err) {
461461
//logRunEvent({'div_id': this.divid, 'code': this.prog, 'errinfo': err.toString()}); // Log the run event
462-
console.log("Runtime Error: " + err.toString());
462+
var errHead = $('<h3>').html('Error')
463+
this.eContainer = this.outerDiv.appendChild(document.createElement('div'))
464+
this.eContainer.className = 'error alert alert-danger';
465+
this.eContainer.id = this.divid + '_errinfo';
466+
this.eContainer.appendChild(errHead[0]);
467+
var errText = this.eContainer.appendChild(document.createElement('pre'))
468+
var errString = err.toString()
469+
var to = errString.indexOf(":")
470+
var errName = errString.substring(0, to);
471+
errText.innerHTML = errString;
472+
$(this.eContainer).append('<h3>Description</h3>');
473+
var errDesc = this.eContainer.appendChild(document.createElement('p'));
474+
errDesc.innerHTML = errorText[errName];
475+
$(this.eContainer).append('<h3>To Fix</h3>');
476+
var errFix = this.eContainer.appendChild(document.createElement('p'));
477+
errFix.innerHTML = errorText[errName + 'Fix'];
478+
var moreInfo = '../ErrorHelp/' + errName.toLowerCase() + '.html';
479+
//console.log("Runtime Error: " + err.toString());
463480
};
464481

482+
483+
484+
var errorText = {};
485+
486+
errorText.ParseError = "A parse error means that Python does not understand the syntax on the line the error message points out. Common examples are forgetting commas beteween arguments or forgetting a : on a for statement";
487+
errorText.ParseErrorFix = "To fix a parse error you just need to look carefully at the line with the error and possibly the line before it. Make sure it conforms to all of Python's rules.";
488+
errorText.TypeError = "Type errors most often occur when an expression tries to combine two objects with types that should not be combined. Like raising a string to a power";
489+
errorText.TypeErrorFix = "To fix a type error you will most likely need to trace through your code and make sure the variables have the types you expect them to have. It may be helpful to print out each variable along the way to be sure its value is what you think it should be.";
490+
errorText.NameError = "A name error almost always means that you have used a variable before it has a value. Often this may be a simple typo, so check the spelling carefully.";
491+
errorText.NameErrorFix = "Check the right hand side of assignment statements and your function calls, this is the most likely place for a NameError to be found.";
492+
errorText.ValueError = "A ValueError most often occurs when you pass a parameter to a function and the function is expecting one type and you pass another.";
493+
errorText.ValueErrorFix = "The error message gives you a pretty good hint about the name of the function as well as the value that is incorrect. Look at the error message closely and then trace back to the variable containing the problematic value.";
494+
errorText.AttributeError = "This error message is telling you that the object on the left hand side of the dot, does not have the attribute or method on the right hand side.";
495+
errorText.AttributeErrorFix = "The most common variant of this message is that the object undefined does not have attribute X. This tells you that the object on the left hand side of the dot is not what you think. Trace the variable back and print it out in various places until you discover where it becomes undefined. Otherwise check the attribute on the right hand side of the dot for a typo.";
496+
errorText.TokenError = "Most of the time this error indicates that you have forgotten a right parenthesis or have forgotten to close a pair of quotes.";
497+
errorText.TokenErrorFix = "Check each line of your program and make sure that your parenthesis are balanced.";
498+
errorText.TimeLimitError = "Your program is running too long. Most programs in this book should run in less than 10 seconds easily. This probably indicates your program is in an infinite loop.";
499+
errorText.TimeLimitErrorFix = "Add some print statements to figure out if your program is in an infinte loop. If it is not you can increase the run time with sys.setExecutionLimit(msecs)";
500+
errorText.Error = "Your program is running for too long. Most programs in this book should run in less than 30 seconds easily. This probably indicates your program is in an infinite loop.";
501+
errorText.ErrorFix = "Add some print statements to figure out if your program is in an infinte loop. If it is not you can increase the run time with sys.setExecutionLimit(msecs)";
502+
errorText.SyntaxError = "This message indicates that Python can't figure out the syntax of a particular statement. Some examples are assigning to a literal, or a function call";
503+
errorText.SyntaxErrorFix = "Check your assignment statments and make sure that the left hand side of the assignment is a variable, not a literal or a function.";
504+
errorText.IndexError = "This message means that you are trying to index past the end of a string or a list. For example if your list has 3 things in it and you try to access the item at position 3 or more.";
505+
errorText.IndexErrorFix = "Remember that the first item in a list or string is at index position 0, quite often this message comes about because you are off by one. Remember in a list of length 3 the last legal index is 2";
506+
errorText.URIError = "";
507+
errorText.URIErrorFix = "";
508+
errorText.ImportError = "This error message indicates that you are trying to import a module that does not exist";
509+
errorText.ImportErrorFix = "One problem may simply be that you have a typo. It may also be that you are trying to import a module that exists in 'real' Python, but does not exist in this book. If this is the case, please submit a feature request to have the module added.";
510+
errorText.ReferenceError = "This is most likely an internal error, particularly if the message references the console.";
511+
errorText.ReferenceErrorFix = "Try refreshing the webpage, and if the error continues, submit a bug report along with your code";
512+
errorText.ZeroDivisionError = "This tells you that you are trying to divide by 0. Typically this is because the value of the variable in the denominator of a division expression has the value 0";
513+
errorText.ZeroDivisionErrorFix = "You may need to protect against dividing by 0 with an if statment, or you may need to rexamine your assumptions about the legal values of variables, it could be an earlier statment that is unexpectedly assigning a value of zero to the variable in question.";
514+
errorText.RangeError = "This message almost always shows up in the form of Maximum call stack size exceeded.";
515+
errorText.RangeErrorFix = "This always occurs when a function calls itself. Its pretty likely that you are not doing this on purpose. Except in the chapter on recursion. If you are in that chapter then its likely you haven't identified a good base case.";
516+
errorText.InternalError = "An Internal error may mean that you've triggered a bug in our Python";
517+
errorText.InternalErrorFix = "Report this error, along with your code as a bug.";
518+
errorText.IndentationError = "This error occurs when you have not indented your code properly. This is most likely to happen as part of an if, for, while or def statement.";
519+
errorText.IndentationErrorFix = "Check your if, def, for, and while statements to be sure the lines are properly indented beneath them. Another source of this error comes from copying and pasting code where you have accidentally left some bits of code lying around that don't belong there anymore.";
520+
errorText.NotImplementedError = "This error occurs when you try to use a builtin function of Python that has not been implemented in this in-browser version of Python.";
521+
errorText.NotImplementedErrorFix = "For now the only way to fix this is to not use the function. There may be workarounds. If you really need this builtin function then file a bug report and tell us how you are trying to use the function.";
522+
523+
524+
525+
465526
ActiveCode.prototype.setTimeLimit = function (timer) {
466527
var timelimit = this.timeLimit;
467528
if (timer !== undefined ) {
@@ -535,7 +596,7 @@ ActiveCode.prototype.runProg = function() {
535596

536597
$(this.output).text('');
537598

538-
599+
$(this.eContainer).remove();
539600
Sk.configure({output : this.outputfun.bind(this),
540601
read : this.builtinRead,
541602
python3: true,

0 commit comments

Comments
 (0)