Skip to content

Commit f2722ad

Browse files
authored
Catch AttributeErrors stemming from ipython_genutils as ValidationErrors on read (#241)
* Catch AttributeErrors thrown by ipython_genutils during reading and conversion as ValidationErrors * fix docstrings * catch attribute errors as validation errors while reading json * revert changes to converter (in another PR)
1 parent c521262 commit f2722ad

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

nbformat/reader.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Distributed under the terms of the Modified BSD License.
55

66
import json
7+
from .validator import ValidationError
78

89
class NotJSONError(ValueError):
910
pass
@@ -52,13 +53,24 @@ def reads(s, **kwargs):
5253
-------
5354
nb : NotebookNode
5455
The notebook that was read.
56+
57+
Raises
58+
------
59+
ValidationError
60+
Notebook JSON for a given version is missing an expected key and cannot be read.
61+
62+
NBFormatError
63+
Specified major version is invalid or unsupported.
5564
"""
5665
from . import versions, NBFormatError
5766

5867
nb_dict = parse_json(s, **kwargs)
5968
(major, minor) = get_version(nb_dict)
6069
if major in versions:
61-
return versions[major].to_notebook_json(nb_dict, minor=minor)
70+
try:
71+
return versions[major].to_notebook_json(nb_dict, minor=minor)
72+
except AttributeError as e:
73+
raise ValidationError(f"The notebook is invalid and is missing an expected key: {e}")
6274
else:
6375
raise NBFormatError('Unsupported nbformat version %s' % major)
6476

@@ -80,3 +92,4 @@ def read(fp, **kwargs):
8092
The notebook that was read.
8193
"""
8294
return reads(fp.read(), **kwargs)
95+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"metadata": {
3+
"name": ""
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"metadata": {
3+
"name": ""
4+
},
5+
"nbformat": 3,
6+
"nbformat_minor": 0,
7+
"worksheets": [
8+
{
9+
"metadata": {}
10+
}
11+
]
12+
}

nbformat/tests/test_reader.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .base import TestsBase
1616

1717
from ..reader import read, get_version
18+
from ..validator import ValidationError
1819

1920
#-----------------------------------------------------------------------------
2021
# Classes and functions
@@ -36,3 +37,14 @@ def test_read(self):
3637
nb = read(f)
3738
(major, minor) = get_version(nb)
3839
self.assertEqual(major, 2)
40+
41+
def test_read_fails_on_missing_worksheets(self):
42+
with self.fopen(u'test3_no_worksheets.ipynb', u'r') as f:
43+
with self.assertRaisesRegex(ValidationError, r'worksheets'):
44+
nb = read(f)
45+
46+
def test_read_fails_on_missing_worksheet_cells(self):
47+
with self.fopen(u'test3_worksheet_with_no_cells.ipynb', u'r') as f:
48+
with self.assertRaisesRegex(ValidationError, r'cells'):
49+
nb = read(f)
50+

0 commit comments

Comments
 (0)