Skip to content

Commit

Permalink
add human-readable cli request error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-dot committed Mar 7, 2024
1 parent 7bbc94e commit 0f20d3b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/blueapi/cli/rest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any, Callable, Literal, Mapping, Optional, Type, TypeVar

from http import HTTPStatus

import requests
from pydantic import parse_obj_as

Expand All @@ -23,6 +25,15 @@ def _is_exception(response: requests.Response) -> bool:
return response.status_code >= 400


def get_status_message(code: int) -> str:
"""Returns the standard description for a given HTTP status code."""
try:
message = HTTPStatus(code).phrase
return message
except ValueError:
return "Unknown Status Code"

Check warning on line 34 in src/blueapi/cli/rest.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/rest.py#L30-L34

Added lines #L30 - L34 were not covered by tests


class BlueapiRestClient:
_config: RestConfig

Expand Down Expand Up @@ -106,7 +117,10 @@ def _request_and_deserialize(
url = self._url(suffix)
response = requests.request(method, url, json=data)
if raise_if(response):
raise BlueskyRemoteError(str(response))
message = get_status_message(response.status_code)
t = response.text
error_message = f"Response failed with text: {t}, with error code: {response.status_code} which corresponds to {message}"
raise BlueskyRemoteError(error_message)

Check warning on line 123 in src/blueapi/cli/rest.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/rest.py#L120-L123

Added lines #L120 - L123 were not covered by tests
deserialized = parse_obj_as(target_type, response.json())
return deserialized

Expand Down

0 comments on commit 0f20d3b

Please sign in to comment.