aiondemand / aiondemand/aiondemand
Bug: get_current_user() crashes with unhandled exception on non-200/401 server responses
- Vorherrschende Sprache
- Python
- Sterne
- 38
- Forks
- 89
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
**Describe the bug**
In `src/aiod/authentication/authentication.py`, `get_current_user()`
only handles `HTTPStatus.UNAUTHORIZED (401)` responses:
content = response.json()
if response.status_code == http.client.UNAUTHORIZED:
raise NotAuthenticatedError(content)
return User(...)
If the server returns any other error (e.g. 500, 503, 404),
`response.json()` is called blindly and the function attempts to
access `content["name"]` and `content["roles"]` on an error response
dict, crashing with a confusing `KeyError` instead of a meaningful error.
**To Reproduce**
Any server-side error (500, 503) while calling `get_current_user()`
will produce an unhandled `KeyError` instead of a clear error message.
**Expected behavior**
A clear `ServerError` or `RuntimeError` with the status code and detail,
consistent with how other functions in `calls.py` handle server errors.
**Actual behavior**
Unhandled `KeyError: 'name'` or `KeyError: 'roles'` crash.
**Proposed Fix**
```python
content = response.json()
if response.status_code == http.client.UNAUTHORIZED:
raise NotAuthenticatedError(content)
if response.status_code != http.client.OK:
raise RuntimeError(
f"Unexpected server response {response.status_code}: {content}"
)
return User(
name=content["name"],
roles=tuple(content["roles"]),
)
```
**Environment**
- aiondemand version: 0.2.5
- Python version: 3.x
Beitragsleitfaden
Bewertung
Dieses Issue wurde noch nicht bewertet.