From 25546b59f8c6622251e4f474415e3c9586a9f160 2013-09-09 00:24:19 From: MinRK Date: 2013-09-09 00:24:19 Subject: [PATCH] Backport PR #4136: catch javascript errors in any output not just javascript. catches js errors appending anything to the output area, such as js in HTML (#4095). closes #4095 --- diff --git a/IPython/html/static/notebook/js/outputarea.js b/IPython/html/static/notebook/js/outputarea.js index 5ed8152..4434a60 100644 --- a/IPython/html/static/notebook/js/outputarea.js +++ b/IPython/html/static/notebook/js/outputarea.js @@ -313,6 +313,33 @@ var IPython = (function (IPython) { return oa; }; + OutputArea.prototype._append_javascript_error = function (err, container) { + // display a message when a javascript error occurs in display output + var msg = "Javascript error adding output!" + console.log(msg, err); + if ( container === undefined ) return; + container.append( + $('
').html(msg + "
" + + err.toString() + + '
See your browser Javascript console for more details.' + ).addClass('js-error') + ); + container.show(); + }; + + OutputArea.prototype._safe_append = function (toinsert) { + // safely append an item to the document + // this is an object created by user code, + // and may have errors, which should not be raised + // under any circumstances. + try { + this.element.append(toinsert); + } catch(err) { + console.log(err); + this._append_javascript_error(err, this.element); + } + }; + OutputArea.prototype.append_pyout = function (json, dynamic) { var n = json.prompt_number || ' '; @@ -321,19 +348,7 @@ var IPython = (function (IPython) { toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); } this.append_mime_type(json, toinsert, dynamic); - try { - this.element.append(toinsert); - } catch(err) { - console.log("Error attaching output!"); - console.log(err); - this.element.show(); - toinsert.html($('
') - .html("Javascript error adding output!
" + - err.toString() + - '
See your browser Javascript console for more details.') - .addClass('js-error') - ); - } + this._safe_append(toinsert); // If we just output latex, typeset it. if ((json.latex !== undefined) || (json.html !== undefined)) { this.typeset(); @@ -352,7 +367,7 @@ var IPython = (function (IPython) { s = s + '\n'; var toinsert = this.create_output_area(); this.append_text(s, {}, toinsert); - this.element.append(toinsert); + this._safe_append(toinsert); } }; @@ -389,14 +404,14 @@ var IPython = (function (IPython) { // If we got here, attach a new div var toinsert = this.create_output_area(); this.append_text(text, {}, toinsert, "output_stream "+subclass); - this.element.append(toinsert); + this._safe_append(toinsert); }; OutputArea.prototype.append_display_data = function (json, dynamic) { var toinsert = this.create_output_area(); this.append_mime_type(json, toinsert, dynamic); - this.element.append(toinsert); + this._safe_append(toinsert); // If we just output latex, typeset it. if ( (json.latex !== undefined) || (json.html !== undefined) ) { this.typeset(); @@ -444,15 +459,7 @@ var IPython = (function (IPython) { try { eval(js); } catch(err) { - console.log('Error in Javascript!'); - console.log(err); - container.show(); - element.append($('
') - .html("Error in Javascript !
"+ - err.toString()+ - '
See your browser Javascript console for more details.') - .addClass('js-error') - ); + this._append_javascript_error(err, container); } };