Skip to content

Commit 9512038

Browse files
committed
Make it clear when it's a SchemaError vs ValidationError even in the message.
Closes: #352.
1 parent cee8c83 commit 9512038

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

jsonschema/exceptions.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,18 @@ def __unicode__(self):
6767
pinstance = pprint.pformat(self.instance, width=72)
6868
return self.message + textwrap.dedent("""
6969
70-
Failed validating %r in schema%s:
70+
Failed validating %r in %s%s:
7171
%s
7272
73-
On instance%s:
73+
On %s%s:
7474
%s
7575
""".rstrip()
7676
) % (
7777
self.validator,
78+
self._word_for_schema_in_error_message,
7879
_utils.format_as_index(list(self.relative_schema_path)[:-1]),
7980
_utils.indent(pschema),
81+
self._word_for_instance_in_error_message,
8082
_utils.format_as_index(self.relative_path),
8183
_utils.indent(pinstance),
8284
)
@@ -125,11 +127,13 @@ def _contents(self):
125127

126128

127129
class ValidationError(_Error):
128-
pass
130+
_word_for_schema_in_error_message = "schema"
131+
_word_for_instance_in_error_message = "instance"
129132

130133

131134
class SchemaError(_Error):
132-
pass
135+
_word_for_schema_in_error_message = "metaschema"
136+
_word_for_instance_in_error_message = "schema"
133137

134138

135139
class RefResolutionError(Exception):

jsonschema/tests/test_validators.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from contextlib import contextmanager
33
import json
44

5-
from jsonschema import FormatChecker, ValidationError
5+
from jsonschema import FormatChecker, SchemaError, ValidationError
66
from jsonschema.tests.compat import mock, unittest
77
from jsonschema.validators import (
88
RefResolutionError, UnknownType, Draft3Validator,
@@ -782,6 +782,22 @@ def test_draft4_validator_is_the_default(self):
782782
validate({}, {})
783783
chk_schema.assert_called_once_with({})
784784

785+
def test_validation_error_message(self):
786+
with self.assertRaises(ValidationError) as e:
787+
validate(12, {"type": "string"})
788+
self.assertRegexpMatches(
789+
str(e.exception),
790+
"(?s)Failed validating u?'.*' in schema.*On instance",
791+
)
792+
793+
def test_schema_error_message(self):
794+
with self.assertRaises(SchemaError) as e:
795+
validate(12, {"type": 12})
796+
self.assertRegexpMatches(
797+
str(e.exception),
798+
"(?s)Failed validating u?'.*' in metaschema.*On schema",
799+
)
800+
785801

786802
class TestRefResolver(unittest.TestCase):
787803

0 commit comments

Comments
 (0)