Files
dzentra_bot/app/src/integrations/exchange/exceptions.py

53 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# app/src/integrations/exchange/exceptions.py
from __future__ import annotations
class ExchangeError(Exception):
"""Base exchange integration error."""
class ExchangeConnectionError(ExchangeError):
"""HTTP/network/timeout level exchange error."""
class ExchangeResponseError(ExchangeError):
"""Unexpected HTTP response or malformed JSON."""
def is_exchange_time_sync_error(exc: Exception) -> bool:
text = str(exc).lower()
return (
"-1021" in text
or "doesn't match server time" in text
or "server time" in text and "match" in text
or "рассинхрон" in text
)
def format_exchange_error_for_user(exc: Exception) -> str:
if is_exchange_time_sync_error(exc):
return (
"Биржа отклонила запрос из-за рассинхронизации времени. "
"Проверь системное время и повтори попытку."
)
if isinstance(exc, ExchangeConnectionError):
return (
"Не удалось получить данные биржи: таймаут или ошибка сети. "
"Попробуй ещё раз через несколько секунд."
)
if isinstance(exc, ExchangeResponseError):
return (
"Биржа вернула ошибку ответа. "
"Попробуй ещё раз через несколько секунд."
)
if isinstance(exc, ExchangeError):
return (
"Не удалось получить данные биржи. "
"Попробуй ещё раз через несколько секунд."
)
return "Временная ошибка получения данных биржи. Попробуй ещё раз через несколько секунд."