Skip to content

Unhandled Exception on Request Failure to Resolve $ref #1053

Closed as not planned
@firyalff

Description

@firyalff

Background

No try catch on request call causing unhandled exception which can cause application error. See image below for server log on remote resolve error occurrence. In this case, one of ref API returns HTTP 502 on JSON Schema $ref resolve process

Screen Shot 2023-03-08 at 15 56 56

Proposal to Resolve

Looking at piece of code from this library on https://github.com/python-jsonschema/jsonschema/blob/main/jsonschema/validators.py#L975

try:
    import requests
except ImportError:
    requests = None

scheme = urlsplit(uri).scheme

if scheme in self.handlers:
    result = self.handlers[scheme](uri)
elif scheme in ["http", "https"] and requests:
    # Requests has support for detecting the correct encoding of
    # json over http
    result = requests.get(uri).json()
else:
    # Otherwise, pass off to urllib and assume utf-8
    with urlopen(uri) as url:
        result = json.loads(url.read().decode("utf-8"))

if self.cache_remote:
    self.store[uri] = result
return result

Try/Catch should be wrapping this part of code

result = requests.get(uri).json()

to ensure that error on request (e.g. 5xx HTTP response) can be handled gracefully.

If this is OK, I'd be glad to open PR for fix

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions