From 562de343c96fa3ef0eedfc84a1aa7263816a6fb8 2013-07-25 07:11:39 From: Matthias Bussonnier Date: 2013-07-25 07:11:39 Subject: [PATCH] Merge pull request #3741 from minrk/badnbmsg better message when notebook format is not supported different messages for bad version and bad JSON. closes #1592 --- diff --git a/IPython/html/services/notebooks/filenbmanager.py b/IPython/html/services/notebooks/filenbmanager.py index 3c14909..ddb6835 100644 --- a/IPython/html/services/notebooks/filenbmanager.py +++ b/IPython/html/services/notebooks/filenbmanager.py @@ -146,8 +146,9 @@ class FileNotebookManager(NotebookManager): try: # v1 and v2 and json in the .ipynb files. nb = current.reads(s, u'json') - except Exception as e: - raise web.HTTPError(500, u'Unreadable JSON notebook: %s' % e) + except ValueError as e: + msg = u"Unreadable Notebook: %s" % e + raise web.HTTPError(400, msg, reason=msg) return last_modified, nb def read_notebook_object(self, notebook_id): diff --git a/IPython/html/static/notebook/js/notebook.js b/IPython/html/static/notebook/js/notebook.js index 08981fb..52bfdc7 100644 --- a/IPython/html/static/notebook/js/notebook.js +++ b/IPython/html/static/notebook/js/notebook.js @@ -1794,20 +1794,20 @@ var IPython = (function (IPython) { * @param {String} errorThrow HTTP error message */ Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) { - if (xhr.status === 500) { - var msg = "An error occurred while loading this notebook. Most likely " + - "this notebook is in a newer format than is supported by this " + - "version of IPython. This version can load notebook formats " + - "v"+this.nbformat+" or earlier."; - - IPython.dialog.modal({ - title: "Error loading notebook", - body : msg, - buttons : { - "OK": {} - } - }); + if (xhr.status === 400) { + var msg = errorThrow; + } else if (xhr.status === 500) { + var msg = "An unknown error occurred while loading this notebook. " + + "This version can load notebook formats " + + "v" + this.nbformat + " or earlier."; } + IPython.dialog.modal({ + title: "Error loading notebook", + body : msg, + buttons : { + "OK": {} + } + }); } /********************* checkpoint-related *********************/ diff --git a/IPython/nbformat/current.py b/IPython/nbformat/current.py index 6f89356..f5862bf 100644 --- a/IPython/nbformat/current.py +++ b/IPython/nbformat/current.py @@ -40,14 +40,19 @@ current_nbformat = nbformat current_nbformat_minor = nbformat_minor +class NBFormatError(ValueError): + pass -class NBFormatError(Exception): +class NotJSONError(ValueError): pass def parse_json(s, **kwargs): """Parse a string into a (nbformat, dict) tuple.""" - d = json.loads(s, **kwargs) + try: + d = json.loads(s, **kwargs) + except ValueError: + raise NotJSONError("Notebook does not appear to be JSON: %r" % s[:16]) nbf = d.get('nbformat', 1) nbm = d.get('nbformat_minor', 0) return nbf, nbm, d @@ -82,7 +87,7 @@ def reads_json(s, **kwargs): nb = v3.to_notebook_json(d, **kwargs) nb = v3.convert_to_this_nbformat(nb, orig_version=3, orig_minor=minor) else: - raise NBFormatError('Unsupported JSON nbformat version: %i' % nbf) + raise NBFormatError('Unsupported JSON nbformat version %s (supported version: %i)' % (nbf, 3)) return nb