From a9c12437b907c9ed2f9800e2d651422c70335ee4 2013-07-23 19:36:22
From: MinRK <benjaminrk@gmail.com>
Date: 2013-07-23 19:36:22
Subject: [PATCH] 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