mirror of
https://git.pleroma.social/pleroma/relay.git
synced 2024-11-25 16:01:09 +00:00
change how HttpClient.get
handles errors
* raise `blib.HttpError` on non 200 and 202 statuses * use `EmptyBodyError` instead of `ValueError` for empty bodies
This commit is contained in:
parent
619b1d5560
commit
ca70c1b293
2
relay/errors.py
Normal file
2
relay/errors.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
class EmptyBodyError(Exception):
|
||||||
|
pass
|
|
@ -4,12 +4,13 @@ import json
|
||||||
|
|
||||||
from aiohttp import ClientSession, ClientTimeout, TCPConnector
|
from aiohttp import ClientSession, ClientTimeout, TCPConnector
|
||||||
from aputils import AlgorithmType, Nodeinfo, ObjectType, Signer, WellKnownNodeinfo
|
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 typing import TYPE_CHECKING, Any, TypeVar, overload
|
||||||
|
|
||||||
from . import __version__, logger as logging
|
from . import __version__, logger as logging
|
||||||
from .cache import Cache
|
from .cache import Cache
|
||||||
from .database.schema import Instance
|
from .database.schema import Instance
|
||||||
|
from .errors import EmptyBodyError
|
||||||
from .misc import MIMETYPES, Message, get_app
|
from .misc import MIMETYPES, Message, get_app
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
@ -107,11 +108,7 @@ class HttpClient:
|
||||||
if not self._session:
|
if not self._session:
|
||||||
raise RuntimeError('Client not open')
|
raise RuntimeError('Client not open')
|
||||||
|
|
||||||
try:
|
url = url.split("#", 1)[0]
|
||||||
url, _ = url.split('#', 1)
|
|
||||||
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if not force:
|
if not force:
|
||||||
try:
|
try:
|
||||||
|
@ -136,10 +133,17 @@ class HttpClient:
|
||||||
|
|
||||||
data = await resp.text()
|
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.verbose('Received error when requesting %s: %i', url, resp.status)
|
||||||
logging.debug(data)
|
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')
|
self.cache.set('request', url, data, 'str')
|
||||||
return data
|
return data
|
||||||
|
@ -151,7 +155,7 @@ class HttpClient:
|
||||||
sign_headers: bool,
|
sign_headers: bool,
|
||||||
cls: None = None,
|
cls: None = None,
|
||||||
force: bool = False,
|
force: bool = False,
|
||||||
old_algo: bool = True) -> None: ...
|
old_algo: bool = True) -> str | None: ...
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
@ -168,7 +172,7 @@ class HttpClient:
|
||||||
sign_headers: bool,
|
sign_headers: bool,
|
||||||
cls: type[T] | None = None,
|
cls: type[T] | None = None,
|
||||||
force: bool = False,
|
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):
|
if cls is not None and not issubclass(cls, JsonBase):
|
||||||
raise TypeError('cls must be a sub-class of "blib.JsonBase"')
|
raise TypeError('cls must be a sub-class of "blib.JsonBase"')
|
||||||
|
@ -177,11 +181,12 @@ class HttpClient:
|
||||||
|
|
||||||
if cls is not None:
|
if cls is not None:
|
||||||
if data is 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 cls.parse(data)
|
||||||
|
|
||||||
return None
|
return data
|
||||||
|
|
||||||
|
|
||||||
async def post(self, url: str, data: Message | bytes, instance: Instance | None = None) -> None:
|
async def post(self, url: str, data: Message | bytes, instance: Instance | None = None) -> None:
|
||||||
|
|
Loading…
Reference in a new issue