From c482677c32c5637baecf1f3abb3edf0ebed4fa8e Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Sun, 15 Sep 2024 07:34:09 -0400 Subject: [PATCH] raise error on non 200 or 202 status in `HttpClient.post` --- relay/http_client.py | 16 ++++++---------- relay/workers.py | 4 ++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/relay/http_client.py b/relay/http_client.py index 8188712..8548dcc 100644 --- a/relay/http_client.py +++ b/relay/http_client.py @@ -220,16 +220,12 @@ class HttpClient: logging.verbose('Sending "%s" to %s', mtype, url) async with self._session.post(url, headers = headers, data = body) as resp: - # Not expecting a response, so just return - if resp.status in {200, 202}: - logging.verbose('Successfully sent "%s" to %s', mtype, url) - return - - logging.error('Received error when pushing to %s: %i', url, resp.status) - logging.debug(await resp.read()) - logging.debug("message: %s", body.decode("utf-8")) - logging.debug("headers: %s", json.dumps(headers, indent = 4)) - return + if resp.status not in (200, 202): + raise HttpError( + resp.status, + await resp.text(), + headers = {k: v for k, v in resp.headers.items()} + ) async def fetch_nodeinfo(self, domain: str, force: bool = False) -> Nodeinfo: diff --git a/relay/workers.py b/relay/workers.py index 31cf4c3..3a0a022 100644 --- a/relay/workers.py +++ b/relay/workers.py @@ -5,6 +5,7 @@ import traceback from aiohttp.client_exceptions import ClientConnectionError, ClientSSLError from asyncio.exceptions import TimeoutError as AsyncTimeoutError +from blib import HttpError from dataclasses import dataclass from multiprocessing import Event, Process, Queue, Value from multiprocessing.queues import Queue as QueueType @@ -94,6 +95,9 @@ class PushWorker(Process): try: await self.client.post(item.inbox, item.message, item.instance) + except HttpError as e: + logging.error('HTTP Error when pushing to %s: %i %s', item.inbox, e.status, e.message) + except AsyncTimeoutError: logging.error('Timeout when pushing to %s', item.domain)