Skip to content

Commit 19ed819

Browse files
committed
Add _VerboseError
1 parent 2815178 commit 19ed819

File tree

1 file changed

+55
-33
lines changed

1 file changed

+55
-33
lines changed

jsonschema/exceptions.py

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections import defaultdict, deque
77
from pprint import pformat
88
from textwrap import dedent, indent
9+
from typing import ClassVar
910
import heapq
1011
import itertools
1112

@@ -22,7 +23,7 @@
2223
class _Error(Exception):
2324
def __init__(
2425
self,
25-
message,
26+
message: str,
2627
validator=_unset,
2728
path=(),
2829
cause=None,
@@ -64,34 +65,8 @@ def __init__(
6465
def __repr__(self):
6566
return f"<{self.__class__.__name__}: {self.message!r}>"
6667

67-
def __str__(self):
68-
essential_for_verbose = (
69-
self.validator, self.validator_value, self.instance, self.schema,
70-
)
71-
if any(m is _unset for m in essential_for_verbose):
72-
return self.message
73-
74-
schema_path = _utils.format_as_index(
75-
container=self._word_for_schema_in_error_message,
76-
indices=list(self.relative_schema_path)[:-1],
77-
)
78-
instance_path = _utils.format_as_index(
79-
container=self._word_for_instance_in_error_message,
80-
indices=self.relative_path,
81-
)
82-
prefix = 16 * " "
83-
84-
return dedent(
85-
f"""\
86-
{self.message}
87-
88-
Failed validating {self.validator!r} in {schema_path}:
89-
{indent(pformat(self.schema, width=72), prefix).lstrip()}
90-
91-
On {instance_path}:
92-
{indent(pformat(self.instance, width=72), prefix).lstrip()}
93-
""".rstrip(),
94-
)
68+
def __str__(self) -> str:
69+
return self.message
9570

9671
@classmethod
9772
def create_from(cls, other):
@@ -137,8 +112,16 @@ def _set(self, type_checker=None, **kwargs):
137112

138113
def _contents(self):
139114
attrs = (
140-
"message", "cause", "context", "validator", "validator_value",
141-
"path", "schema_path", "instance", "schema", "parent",
115+
"message",
116+
"cause",
117+
"context",
118+
"validator",
119+
"validator_value",
120+
"path",
121+
"schema_path",
122+
"instance",
123+
"schema",
124+
"parent",
142125
)
143126
return dict((attr, getattr(self, attr)) for attr in attrs)
144127

@@ -157,7 +140,44 @@ def _matches_type(self):
157140
)
158141

159142

160-
class ValidationError(_Error):
143+
class _VerboseError(_Error):
144+
_word_for_schema_in_error_message: ClassVar[str]
145+
_word_for_instance_in_error_message: ClassVar[str]
146+
147+
def __str__(self):
148+
essential_for_verbose = (
149+
self.validator,
150+
self.validator_value,
151+
self.instance,
152+
self.schema,
153+
)
154+
if any(m is _unset for m in essential_for_verbose):
155+
return self.message
156+
157+
schema_path = _utils.format_as_index(
158+
container=self._word_for_schema_in_error_message,
159+
indices=list(self.relative_schema_path)[:-1],
160+
)
161+
instance_path = _utils.format_as_index(
162+
container=self._word_for_instance_in_error_message,
163+
indices=self.relative_path,
164+
)
165+
prefix = 16 * " "
166+
167+
return dedent(
168+
f"""\
169+
{self.message}
170+
171+
Failed validating {self.validator!r} in {schema_path}:
172+
{indent(pformat(self.schema, width=72), prefix).lstrip()}
173+
174+
On {instance_path}:
175+
{indent(pformat(self.instance, width=72), prefix).lstrip()}
176+
""".rstrip(),
177+
)
178+
179+
180+
class ValidationError(_VerboseError):
161181
"""
162182
An instance was invalid under a provided schema.
163183
"""
@@ -166,7 +186,7 @@ class ValidationError(_Error):
166186
_word_for_instance_in_error_message = "instance"
167187

168188

169-
class SchemaError(_Error):
189+
class SchemaError(_VerboseError):
170190
"""
171191
A schema was invalid under its corresponding metaschema.
172192
"""
@@ -328,6 +348,7 @@ def by_relevance(weak=WEAK_MATCHES, strong=STRONG_MATCHES):
328348
a collection of validation keywords to consider to be
329349
"strong"
330350
"""
351+
331352
def relevance(error):
332353
validator = error.validator
333354
return (
@@ -336,6 +357,7 @@ def relevance(error):
336357
validator in strong,
337358
not error._matches_type(),
338359
)
360+
339361
return relevance
340362

341363

0 commit comments

Comments
 (0)