fix: an unavailable Tiny (5xx) retries the whole delivery instead of sending the order without its seller

The seller cannot be set after the order is made (order 151250), so a server
error while reading the contact now waits for the outbox's next try, as a
429 already did. A reading Tiny refuses for good still lets the order go,
and now reaches Sentry instead of a warning no one sees.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-27 14:48:34 -03:00
parent 742ea5f5bd
commit 194797c412
2 changed files with 33 additions and 8 deletions

View File

@@ -195,6 +195,11 @@ class TinyRateLimit(TinyError):
transient = True
class TinyUnavailable(TinyError):
"""Tiny answered with a server error (5xx): down for a moment, so what
depends on the answer waits for the outbox's next try."""
class TinyNotConnected(TinyError):
"""No authorised connection yet: an operator must click "Conectar Tiny"."""
@@ -822,6 +827,8 @@ class TinyOrders:
response = self.http.request(method, path, headers=headers, **kwargs)
if response.status_code == 429:
raise TinyRateLimit('Tiny rate limit reached; the outbox will retry')
if response.status_code >= 500:
raise TinyUnavailable(f'{method} {path}: {response.status_code} {response.text[:300]}')
if response.status_code >= 400:
raise TinyError(f'{method} {path}: {response.status_code} {response.text[:300]}')
return response.json() if response.content else {}
@@ -870,14 +877,17 @@ class TinyOrders:
def contact_seller(self, contact_id):
"""The seller (vendedor) in a contact's registration, or None. A
failed reading leaves the order without one: it must still go."""
reading Tiny refuses leaves the order without one: it must still go.
The seller cannot be set after the order is made, so a busy or
unavailable Tiny (429, 5xx, no answer) tries the whole delivery again."""
try:
seller = (self.request('GET', f'/contatos/{contact_id}').get('vendedor') or {}).get('id')
except TinyRateLimit:
# Busy for a moment: the whole delivery is tried again, the seller with it.
except (TinyRateLimit, TinyUnavailable):
raise
except TinyError as exc:
logging.warning('Tiny: could not read the seller of contact %s: %s', contact_id, exc)
# The order goes without its seller, for good: Sentry hears of it.
logging.error('Tiny: could not read the seller of contact %s; the order goes without one: %s',
contact_id, exc)
return None
return int(seller) if str(seller or '').isdigit() and int(seller) > 0 else None