File size: 1,204 Bytes
48d895c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | """Shared helper: map an UpstreamError to the correct ProxyFeedbackKind.
All transport modules (assets, media, livekit, imagine_ws …) use this so the
mapping stays consistent and clearance bundles are properly invalidated.
Rules
-----
401 → UNAUTHORIZED (invalidates clearance bundle)
403 → CHALLENGE (invalidates clearance bundle — treat all 403s as potential CF)
429 → RATE_LIMITED
≥500 → UPSTREAM_5XX
else → TRANSPORT_ERROR
"""
from app.platform.errors import UpstreamError
from app.control.proxy.models import ProxyFeedback, ProxyFeedbackKind
def upstream_feedback(exc: UpstreamError) -> ProxyFeedback:
"""Return a ``ProxyFeedback`` for an ``UpstreamError`` response."""
status = exc.status or 0
if status == 401:
kind = ProxyFeedbackKind.UNAUTHORIZED
elif status == 403:
kind = ProxyFeedbackKind.CHALLENGE
elif status == 429:
kind = ProxyFeedbackKind.RATE_LIMITED
elif status >= 500:
kind = ProxyFeedbackKind.UPSTREAM_5XX
else:
kind = ProxyFeedbackKind.TRANSPORT_ERROR
return ProxyFeedback(kind=kind, status_code=status or None)
__all__ = ["upstream_feedback"]
|