From ca70c1b293003629a340cb78fd0f770b250184c7 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sun, 15 Sep 2024 04:35:00 -0400 Subject: [PATCH] change how `HttpClient.get` handles errors * raise `blib.HttpError` on non 200 and 202 statuses * use `EmptyBodyError` instead of `ValueError` for empty bodies --- relay/errors.py | 2 ++ relay/http_client.py | 29 +++++++++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 relay/errors.py diff --git a/relay/errors.py b/relay/errors.py new file mode 100644 index 0000000..8074929 --- /dev/null +++ b/relay/errors.py @@ -0,0 +1,2 @@ +class EmptyBodyError(Exception): + pass diff --git a/relay/http_client.py b/relay/http_client.py index d15f1d9..ef25881 100644 --- a/relay/http_client.py +++ b/relay/http_client.py @@ -4,12 +4,13 @@ import json from aiohttp import ClientSession, ClientTimeout, TCPConnector from aputils import AlgorithmType, Nodeinfo, ObjectType, Signer, WellKnownNodeinfo -from blib import JsonBase +from blib import HttpError, JsonBase from typing import TYPE_CHECKING, Any, TypeVar, overload from . import __version__, logger as logging from .cache import Cache from .database.schema import Instance +from .errors import EmptyBodyError from .misc import MIMETYPES, Message, get_app if TYPE_CHECKING: @@ -107,11 +108,7 @@ class HttpClient: if not self._session: raise RuntimeError('Client not open') - try: - url, _ = url.split('#', 1) - - except ValueError: - pass + url = url.split("#", 1)[0] if not force: try: @@ -136,10 +133,17 @@ class HttpClient: data = await resp.text() - if resp.status != 200: + if resp.status not in (200, 202): logging.verbose('Received error when requesting %s: %i', url, resp.status) logging.debug(data) - return None + + try: + error = json.loads(data)["error"] + + except Exception: + error = data + + raise HttpError(resp.status, error) self.cache.set('request', url, data, 'str') return data @@ -151,7 +155,7 @@ class HttpClient: sign_headers: bool, cls: None = None, force: bool = False, - old_algo: bool = True) -> None: ... + old_algo: bool = True) -> str | None: ... @overload @@ -168,7 +172,7 @@ class HttpClient: sign_headers: bool, cls: type[T] | None = None, force: bool = False, - old_algo: bool = True) -> T | None: + old_algo: bool = True) -> T | str | None: if cls is not None and not issubclass(cls, JsonBase): raise TypeError('cls must be a sub-class of "blib.JsonBase"') @@ -177,11 +181,12 @@ class HttpClient: if cls is not None: if data is None: - raise ValueError("Empty response") + # this shouldn't actually get raised, but keeping just in case + raise EmptyBodyError(f"GET {url}") return cls.parse(data) - return None + return data async def post(self, url: str, data: Message | bytes, instance: Instance | None = None) -> None: