Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

if .json() fails, error is not captured nicely #5

Description

@jkkr2

Hi, thanks for providing the devpi-tools. I found an issue working with devpi-tool in a network environment, where a firewall is in charge and cannot be deactivated. In __init__.py in class DevpiClient in the get_json method in line 24
res = self.request(method, url, headers=headers, verify=verify_ssl).json()
I get a json parsing error, beacuse our firewall is sending back a blockpage in html in self.request(method, url, headers=headers, verify=verify_ssl).text. This error is captured downstrem in the json parser, but it would be nice to cover it in get_json.

This is my modified version:

class DevpiClient(requests.Session):
    """ A very small client for connecting to devpi web API """
    def __init__(self, base_url, disable_ssl_warning=False):
        super(DevpiClient, self).__init__()
        self.base_url = base_url
        self.disable_ssl_warning = disable_ssl_warning

    def get_json(self, path, method='GET', **params):
        url = self.base_url + path
        headers = {'Accept': 'application/json'}
        verify_ssl = True

        if self.disable_ssl_warning:
            requests.packages.urllib3.disable_warnings()
            verify_ssl = False
        res_raw = self.request(method, url, headers=headers, verify=verify_ssl)
        try:
            res = res_raw.json()
        except:
            raise DevpiApiError(f'Result is not in json format: {res_raw.text}')
        if 'message' in res.keys():
            raise DevpiApiError(res['message'])

        return res['result']

For shure not elegant, but maybe you can implement something like this.

BR,
Johannes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions