Skip to content

Commit 1397f0b

Browse files
committed
Merge remote-tracking branch 'sontek/give_better_error_output'
* sontek/give_better_error_output: Better Py2/3 support Use pretty formatting Cleaned up output Clean up the output when an unknow type is found
2 parents 3906ee0 + 4e6bf80 commit 1397f0b

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

jsonschema/exceptions.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,31 @@ class RefResolutionError(Exception):
9393

9494

9595
class UnknownType(Exception):
96-
pass
96+
def __init__(self, type, instance, schema):
97+
self.type = type
98+
self.instance = instance
99+
self.schema = schema
100+
101+
def __str__(self):
102+
return unicode(self).encode("utf-8")
103+
104+
def __unicode__(self):
105+
pschema = pprint.pformat(self.schema, width=72)
106+
pinstance = pprint.pformat(self.instance, width=72)
107+
return textwrap.dedent("""
108+
Unknown Type: %r, in schema:
109+
%s
110+
111+
On instance:
112+
%s
113+
""".rstrip()
114+
) % (self.type,
115+
_utils.indent(pschema),
116+
_utils.indent(pinstance))
117+
118+
if PY3:
119+
__str__ = __unicode__
120+
97121

98122

99123
class FormatError(Exception):

jsonschema/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def validate(self, *args, **kwargs):
118118

119119
def is_type(self, instance, type):
120120
if type not in self._types:
121-
raise UnknownType(type)
121+
raise UnknownType(type, instance, self.schema)
122122
pytypes = self._types[type]
123123

124124
# bool inherits from int, so ensure bools aren't reported as ints

0 commit comments

Comments
 (0)