149 lines
4.8 KiB
Python
149 lines
4.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any, Mapping, Optional
|
|
|
|
from .config import TOKEN_URL, WEBSITE_URL
|
|
from .i18n import tr
|
|
|
|
|
|
class GeoDonkeyError(Exception):
|
|
def __init__(self, message: str, *, status: Optional[int] = None,
|
|
code: str = "", detail: str = "",
|
|
retry_after: Optional[int] = None) -> None:
|
|
super().__init__(message)
|
|
self.message = message
|
|
self.status = status
|
|
self.code = code
|
|
self.detail = detail
|
|
self.retry_after = retry_after
|
|
|
|
def __str__(self) -> str:
|
|
return self.message
|
|
|
|
|
|
class NetworkError(GeoDonkeyError):
|
|
pass
|
|
|
|
|
|
class AuthError(GeoDonkeyError):
|
|
pass
|
|
|
|
|
|
class CoinsError(GeoDonkeyError):
|
|
pass
|
|
|
|
|
|
class RateLimitError(GeoDonkeyError):
|
|
pass
|
|
|
|
|
|
class RequestError(GeoDonkeyError):
|
|
pass
|
|
|
|
|
|
def _server_text(payload: Any) -> str:
|
|
if isinstance(payload, Mapping):
|
|
error = payload.get("error")
|
|
if isinstance(error, Mapping):
|
|
for key in ("message", "detail"):
|
|
value = error.get(key)
|
|
if isinstance(value, str) and value.strip():
|
|
return value.strip()
|
|
detail = payload.get("detail")
|
|
if isinstance(detail, str) and detail.strip():
|
|
return detail.strip()
|
|
if isinstance(detail, list) and detail:
|
|
first = detail[0]
|
|
if isinstance(first, Mapping) and isinstance(first.get("msg"), str):
|
|
return first["msg"]
|
|
if isinstance(payload, str) and payload.strip():
|
|
return payload.strip()[:400]
|
|
return ""
|
|
|
|
|
|
def _hint(payload: Any) -> str:
|
|
if isinstance(payload, Mapping):
|
|
error = payload.get("error")
|
|
if isinstance(error, Mapping) and isinstance(error.get("hint"), str):
|
|
return error["hint"].strip()
|
|
return ""
|
|
|
|
|
|
def _code(payload: Any) -> str:
|
|
if isinstance(payload, Mapping):
|
|
error = payload.get("error")
|
|
if isinstance(error, Mapping) and isinstance(error.get("code"), str):
|
|
return error["code"]
|
|
return ""
|
|
|
|
|
|
def _retry_after(payload: Any, headers: Optional[Mapping[str, str]]) -> Optional[int]:
|
|
if headers:
|
|
raw = headers.get("retry-after") or headers.get("Retry-After")
|
|
if raw:
|
|
try:
|
|
return max(0, int(float(raw)))
|
|
except (TypeError, ValueError):
|
|
pass
|
|
if isinstance(payload, Mapping):
|
|
error = payload.get("error")
|
|
if isinstance(error, Mapping):
|
|
raw = error.get("retry_after_seconds")
|
|
if isinstance(raw, (int, float)):
|
|
return max(0, int(raw))
|
|
return None
|
|
|
|
|
|
def describe(status: Optional[int], payload: Any = None,
|
|
headers: Optional[Mapping[str, str]] = None,
|
|
url: str = "") -> GeoDonkeyError:
|
|
server = _server_text(payload)
|
|
hint = _hint(payload)
|
|
code = _code(payload)
|
|
wait = _retry_after(payload, headers)
|
|
detail = " · ".join(part for part in (url, server, hint) if part)
|
|
|
|
if status == 401:
|
|
return AuthError(
|
|
tr("The GeoDonkey API key is missing or no longer valid. "
|
|
"Open the plugin settings and paste a key from {url}.").format(url=TOKEN_URL),
|
|
status=status, code=code, detail=detail)
|
|
|
|
if status == 402:
|
|
return CoinsError(
|
|
tr("Not enough DonkeyCoins for this request. "
|
|
"Top up your balance at {url}.").format(url=WEBSITE_URL + "/preise"),
|
|
status=status, code=code, detail=detail)
|
|
|
|
if status == 403:
|
|
return AuthError(
|
|
server or tr("This API key is not allowed to use that endpoint."),
|
|
status=status, code=code, detail=detail)
|
|
|
|
if status == 429:
|
|
if wait:
|
|
message = tr("Rate limit reached. Try again in {seconds} seconds.").format(seconds=wait)
|
|
else:
|
|
message = tr("Rate limit reached. Please wait a moment and try again.")
|
|
return RateLimitError(message, status=status, code=code, detail=detail,
|
|
retry_after=wait)
|
|
|
|
if status in (400, 404, 409, 413, 422):
|
|
return RequestError(
|
|
server or tr("The request was rejected. Please check the query and the area."),
|
|
status=status, code=code, detail=detail)
|
|
|
|
if status is not None and status >= 500:
|
|
return GeoDonkeyError(
|
|
tr("The GeoDonkey service reported a problem. Please try again later."),
|
|
status=status, code=code, detail=detail)
|
|
|
|
if status is None:
|
|
return NetworkError(
|
|
server or tr("No connection to the GeoDonkey service. "
|
|
"Check your internet connection and proxy settings."),
|
|
status=None, code=code, detail=detail)
|
|
|
|
return GeoDonkeyError(server or tr("Unexpected response from the GeoDonkey service."),
|
|
status=status, code=code, detail=detail)
|