From 1963981389d3a8e462af935f9e82759ff17654ae Mon Sep 17 00:00:00 2001 From: John Anderson Date: Thu, 17 Oct 2013 20:19:57 -0700 Subject: Clean up the output when an unknow type is found --- jsonschema/exceptions.py | 23 ++++++++++++++++++++++- jsonschema/validators.py | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index f1d3e42..b24478a 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -93,7 +93,28 @@ class RefResolutionError(Exception): class UnknownType(Exception): - pass + def __init__(self, type, instance, schema): + self.type = type + self.instance = instance + self.schema = schema + self.msg = "OMGZ" + + def __str__(self): + return self.__unicode__() + + def __unicode__(self): + pschema = pprint.pformat(self.schema, width=72) + pinstance = pprint.pformat(self.instance, width=72) + return textwrap.dedent(""" + unknown type %s, in schema: + %r + + On instance: + %s + """.rstrip() + ) % (self.type, self.schema, self.instance) + + class FormatError(Exception): diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 22abf57..7044428 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -118,7 +118,7 @@ def create(meta_schema, validators=(), version=None, default_types=None): # noq def is_type(self, instance, type): if type not in self._types: - raise UnknownType(type) + raise UnknownType(type, instance, self.schema) pytypes = self._types[type] # bool inherits from int, so ensure bools aren't reported as ints -- cgit v1.2.1 From 9440940677ebda454dbff2c5ce4d5376a0dfdb9a Mon Sep 17 00:00:00 2001 From: John Anderson Date: Thu, 17 Oct 2013 20:23:03 -0700 Subject: Cleaned up output --- jsonschema/exceptions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index b24478a..30419ae 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -97,7 +97,6 @@ class UnknownType(Exception): self.type = type self.instance = instance self.schema = schema - self.msg = "OMGZ" def __str__(self): return self.__unicode__() @@ -106,7 +105,7 @@ class UnknownType(Exception): pschema = pprint.pformat(self.schema, width=72) pinstance = pprint.pformat(self.instance, width=72) return textwrap.dedent(""" - unknown type %s, in schema: + Uknown Type: %s, in schema: %r On instance: -- cgit v1.2.1 From a324c1fc19b6ae49da879f804c8e20b8a6d34db7 Mon Sep 17 00:00:00 2001 From: John Anderson Date: Thu, 17 Oct 2013 20:27:27 -0700 Subject: Use pretty formatting --- jsonschema/exceptions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 30419ae..c6e5ad9 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -106,12 +106,14 @@ class UnknownType(Exception): pinstance = pprint.pformat(self.instance, width=72) return textwrap.dedent(""" Uknown Type: %s, in schema: - %r + %s On instance: - %s + %s """.rstrip() - ) % (self.type, self.schema, self.instance) + ) % (self.type, + _utils.indent(pschema), + _utils.indent(pinstance)) -- cgit v1.2.1 From 4e6bf8051eef297d139302205c7fbdece32f49f0 Mon Sep 17 00:00:00 2001 From: John Anderson Date: Fri, 18 Oct 2013 09:52:02 -0700 Subject: Better Py2/3 support --- jsonschema/exceptions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index c6e5ad9..5d49410 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -99,13 +99,13 @@ class UnknownType(Exception): self.schema = schema def __str__(self): - return self.__unicode__() + return unicode(self).encode("utf-8") def __unicode__(self): pschema = pprint.pformat(self.schema, width=72) pinstance = pprint.pformat(self.instance, width=72) return textwrap.dedent(""" - Uknown Type: %s, in schema: + Unknown Type: %r, in schema: %s On instance: @@ -115,6 +115,8 @@ class UnknownType(Exception): _utils.indent(pschema), _utils.indent(pinstance)) + if PY3: + __str__ = __unicode__ -- cgit v1.2.1