From 9ca48f5973dac477098a9ea5acaa6e42f0307587 Mon Sep 17 00:00:00 2001 From: kiriharu Date: Tue, 5 Jan 2021 00:33:55 +0300 Subject: [PATCH] result Response(500) if node unreachable --- apps/tgbot/tgbot/handlers/helpers.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/tgbot/tgbot/handlers/helpers.py b/apps/tgbot/tgbot/handlers/helpers.py index e60fb0b..db9a148 100644 --- a/apps/tgbot/tgbot/handlers/helpers.py +++ b/apps/tgbot/tgbot/handlers/helpers.py @@ -1,4 +1,4 @@ -from httpx import AsyncClient, Timeout +from httpx import AsyncClient, Timeout, Response, ConnectError from typing import List from core.coretypes import APINode @@ -15,8 +15,12 @@ def check_int(value) -> bool: async def send_api_requests(endpoint: str, data: dict, nodes: List[APINode]): for node in nodes: data.update(dict(token=node.token)) - async with AsyncClient(timeout=Timeout(timeout=100.0)) as client: - result = await client.get( - f"{node.address}/{endpoint}", params=data - ) + try: + async with AsyncClient(timeout=Timeout(timeout=100.0)) as client: + result = await client.get( + f"{node.address}/{endpoint}", params=data + ) + except ConnectError: + # We yield 500 response when backend is offline + result = Response(500) yield result