16 أسطر
579 B
Python
16 أسطر
579 B
Python
import httpx
|
|
|
|
|
|
def format_error(prefix: str, e: Exception) -> str:
|
|
if isinstance(e, httpx.HTTPStatusError):
|
|
code = e.response.status_code
|
|
text = e.response.text
|
|
if code in (301, 302, 307, 308):
|
|
return f"{prefix}: {code} - Redirect detected. Probably web UI, not API."
|
|
if "CSRF" in text:
|
|
return f"{prefix}: {code} - CSRF error. Wrong domain."
|
|
if code in (401, 403):
|
|
return f"{prefix}: {code} - Invalid or expired API key."
|
|
return f"{prefix}: {code} - {text}"
|
|
return f"{prefix}: {str(e)}"
|