simonw / dclient

A client CLI utility for Datasette instances
Apache License 2.0
11 stars 0 forks source link

Confusing traceback hitting an endpoint that requires authentication #11

Closed simonw closed 1 year ago

simonw commented 1 year ago
dclient query https://simon.datasette.cloud/data 'select name from sqlite_master'
  File "/Users/simon/Dropbox/Development/dclient/dclient/cli.py", line 41, in query
    data = response.json()
           ^^^^^^^^^^^^^^^
  File "/Users/simon/.local/share/virtualenvs/dclient-N7jtnH1j/lib/python3.11/site-packages/httpx/_models.py", line 756, in json
    return jsonlib.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

In the debugger:

(Pdb) response.status_code
302
(Pdb) response.headers
Headers({'date': 'Mon, 17 Jul 2023 22:07:38 GMT', 'server': 'Fly/a0b91024 (2023-06-13)', 'location': 'https://www.datasette.cloud/login/?next=http%3A%2F%2Fsimon.datasette.cloud%2Fdata.json%3Fsql%3Dselect%2520name%2520from%2520sqlite_master%26_shape%3Dobjects', 'content-type': 'text/plain', 'transfer-encoding': 'chunked', 'content-encoding': 'gzip', 'via': '1.1 fly.io', 'fly-request-id': '01H5JYA4NTXMCB50HHN41VGYT4-lax'})
simonw commented 1 year ago

Anything other than a 200 status code should show a concise Click error message.

simonw commented 1 year ago

Here's the problem: https://github.com/simonw/dclient/blob/055d6a6ffc858e56dda4d43c6f906b90239a4257/dclient/cli.py#L33-L42

It does try to do something else for non-200 errors, but doesn't handle non-JSON responses well.