Skip to content

Commit c8b257b

Browse files
committed
Provide better error messages for failed remote ref resolution.
Closes #82
1 parent 0936a71 commit c8b257b

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

jsonschema.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,10 @@ def resolving(self, ref):
10291029
elif not uri or uri == self.base_uri:
10301030
document = self.referrer
10311031
else:
1032-
document = self.resolve_remote(uri)
1032+
try:
1033+
document = self.resolve_remote(uri)
1034+
except Exception as exc:
1035+
raise RefResolutionError(exc)
10331036

10341037
old_base_uri, old_referrer = self.base_uri, self.referrer
10351038
self.base_uri, self.referrer = uri, document
@@ -1067,7 +1070,8 @@ def resolve_remote(self, uri):
10671070
"""
10681071
Resolve a remote ``uri``.
10691072
1070-
Does not check the store first.
1073+
Does not check the store first, but stores the retrieved document in
1074+
the store if :attr:`RefResolver.cache_remote` is True.
10711075
10721076
.. note::
10731077

tests.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
pypy_version_info = None
2626

2727
from jsonschema import (
28-
PY3, FormatError, SchemaError, UnknownType, ValidationError, ErrorTree,
29-
Draft3Validator, Draft4Validator, FormatChecker, RefResolver,
30-
ValidatorMixin, draft3_format_checker, draft4_format_checker, validate,
28+
PY3, FormatError, RefResolutionError, SchemaError, UnknownType,
29+
ValidationError, ErrorTree, Draft3Validator, Draft4Validator,
30+
FormatChecker, RefResolver, ValidatorMixin, draft3_format_checker,
31+
draft4_format_checker, validate,
3132
)
3233

3334

@@ -715,6 +716,16 @@ def test_cache_remote_off(self):
715716
pass
716717
self.assertEqual(foo_handler.call_count, 2)
717718

719+
def test_if_you_give_it_junk_you_get_a_resolution_error(self):
720+
ref = "foo://bar"
721+
foo_handler = mock.Mock(side_effect=ValueError("Oh no! What's this?"))
722+
resolver = RefResolver("", {}, cache_remote=True,
723+
handlers={"foo": foo_handler})
724+
with self.assertRaises(RefResolutionError) as err:
725+
with resolver.resolving(ref):
726+
pass
727+
self.assertEqual(str(err.exception), "Oh no! What's this?")
728+
718729

719730
class TestFormatChecker(unittest.TestCase):
720731
def setUp(self):

0 commit comments

Comments
 (0)