summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2013-04-19 10:13:39 -0400
committerJulian Berman <Julian@GrayVines.com>2013-04-19 10:13:39 -0400
commit9d4c68ec20e1cb9100d884b556162ba423b3c02a (patch)
treeaebe2e2842790368e25684822c5d0f4acb550b55
parent4f3262df4a7cee8866e796f83a18558fb5b9929d (diff)
downloadjsonschema-9d4c68ec20e1cb9100d884b556162ba423b3c02a.tar.gz
Implement __cause__ for Py3.
Closes: #93
-rw-r--r--jsonschema.py11
-rw-r--r--tests.py20
2 files changed, 22 insertions, 9 deletions
diff --git a/jsonschema.py b/jsonschema.py
index 2a4051f..d1cb280 100644
--- a/jsonschema.py
+++ b/jsonschema.py
@@ -76,7 +76,7 @@ class _Error(Exception):
self.path = collections.deque(path)
self.schema_path = collections.deque(schema_path)
self.context = list(context)
- self.cause = cause
+ self.cause = self.__cause__ = cause
self.validator = validator
self.validator_value = validator_value
self.instance = instance
@@ -118,7 +118,7 @@ class FormatError(Exception):
def __init__(self, message, cause=None):
super(FormatError, self).__init__(message, cause)
self.message = message
- self.cause = cause
+ self.cause = self.__cause__ = cause
def __str__(self):
return self.message.encode("utf-8")
@@ -423,8 +423,8 @@ class _Draft34CommonMixin(object):
):
try:
self.format_checker.check(instance, format)
- except FormatError as e:
- yield ValidationError(unicode(e), cause=e.cause)
+ except FormatError as error:
+ yield ValidationError(error.message, cause=error.cause)
def validate_minLength(self, mL, instance, schema):
if self.is_type(instance, "string") and len(instance) < mL:
@@ -944,7 +944,8 @@ class FormatChecker(object):
cause = e
if not result:
raise FormatError(
- "%r is not a %r" % (instance, format), cause=cause)
+ "%r is not a %r" % (instance, format), cause=cause,
+ )
def conforms(self, instance, format):
"""
diff --git a/tests.py b/tests.py
index e31cea6..05ef02f 100644
--- a/tests.py
+++ b/tests.py
@@ -860,19 +860,31 @@ class TestFormatChecker(unittest.TestCase):
def test_it_catches_registered_errors(self):
checker = FormatChecker()
+ cause = self.fn.side_effect = ValueError()
+
checker.checks("foo", raises=ValueError)(self.fn)
- # Registered errors should be caught and turned into FormatErrors
- cause = ValueError()
- self.fn.side_effect = cause
+
with self.assertRaises(FormatError) as cm:
checker.check("bar", "foo")
- # Original exception should be attached to cause attribute
+
self.assertIs(cm.exception.cause, cause)
+ self.assertIs(cm.exception.__cause__, cause)
+
# Unregistered errors should not be caught
self.fn.side_effect = AttributeError
with self.assertRaises(AttributeError):
checker.check("bar", "foo")
+ def test_format_error_causes_become_validation_error_causes(self):
+ checker = FormatChecker()
+ checker.checks("foo", raises=ValueError)(self.fn)
+ cause = self.fn.side_effect = ValueError()
+ validator = Draft4Validator({"format" : "foo"}, format_checker=checker)
+
+ with self.assertRaises(ValidationError) as cm:
+ validator.validate("bar")
+
+ self.assertIs(cm.exception.__cause__, cause)
def sorted_errors(errors):