Error logging
This commit is contained in:
1 parent
f4c1efb454
commit
6867e93a30
1 file changed
+10
-3
+10
-3
@@ -1,19 +1,26 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
import httpx
|
import httpx
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
logger = logging.getLogger("uvicorn.error")
|
||||||
|
|
||||||
BASE_URL = "https://api.chess.com/pub"
|
BASE_URL = "https://api.chess.com/pub"
|
||||||
HEADERS = {"User-Agent": os.environ.get("CHESSCOM_USER_AGENT", "chess.sistav.com")}
|
HEADERS = {"User-Agent": os.environ.get("USER_AGENT", "chess.sistav.com")}
|
||||||
UPSTREAM_ERROR = "chess.com isn't responding right now. Try again in a minute."
|
UPSTREAM_ERROR = "chess.com isn't responding right now. Try again in a minute."
|
||||||
|
|
||||||
|
|
||||||
async def _get(url: str, timeout: float) -> httpx.Response:
|
async def _get(url: str, timeout: float) -> httpx.Response:
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient(headers=HEADERS, timeout=timeout) as client:
|
async with httpx.AsyncClient(headers=HEADERS, timeout=timeout) as client:
|
||||||
return await client.get(url)
|
resp = await client.get(url)
|
||||||
except httpx.HTTPError:
|
except httpx.HTTPError as exc:
|
||||||
|
logger.warning("chess.com request failed for %s: %r", url, exc)
|
||||||
raise HTTPException(status_code=502, detail=UPSTREAM_ERROR)
|
raise HTTPException(status_code=502, detail=UPSTREAM_ERROR)
|
||||||
|
if resp.status_code not in (200, 404):
|
||||||
|
logger.warning("chess.com returned %s for %s", resp.status_code, url)
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
async def get_archives(username: str) -> list[str]:
|
async def get_archives(username: str) -> list[str]:
|
||||||
|
|||||||
Reference in new issue
Block a user