fix(whatsapp_agent): scope the bad-token heuristic away from media methods
The Developer Manual documents 400/100 as meaning "the API token is present but not valid" only on endpoints without media. Media methods share that same status and code with an unknown or expired media id and with a length cap. Classifying a 400 from `GET /media/<id>` as an invalid token would stop the channel because one attachment expired, so media requests now pass `media_endpoint=True` and the heuristic is skipped for them. A malformed Authorization header (401/190) is still recognised on every endpoint. Also corrects the state module docstring: the store keeps the cursor, dedup window, creator identifier, and delivery receipts, and holds no token material.
This commit is contained in:
1 parent
b7cb5da4b5
commit
4d45564362
4 files changed
+43
-6
No files matched your search
@@ -160,6 +160,10 @@ class APIError:
|
||||
message: str
|
||||
details: str
|
||||
fbtrace_id: str
|
||||
# Media endpoints share 400/100 between an invalid token, an unknown or
|
||||
# expired media id, and a length cap, so a 400 there is not treated as an
|
||||
# authentication failure.
|
||||
media_endpoint: bool = False
|
||||
|
||||
@property
|
||||
def invalid_token(self) -> bool:
|
||||
@@ -167,9 +171,14 @@ class APIError:
|
||||
|
||||
A malformed or missing ``Authorization`` header yields 401/190. A token
|
||||
that is present but not valid yields 400/100 — the API deliberately does
|
||||
not use 401 for that case.
|
||||
not use 401 for that case. That heuristic is scoped to endpoints without
|
||||
media, where 400/100 has no other common cause.
|
||||
"""
|
||||
return self.status_code == 401 or (
|
||||
if self.status_code == 401:
|
||||
return True
|
||||
if self.media_endpoint:
|
||||
return False
|
||||
return (
|
||||
self.status_code == 400
|
||||
and self.code == CODE_INVALID_TOKEN
|
||||
and not self._is_length_rejection
|
||||
@@ -232,7 +241,12 @@ class WhatsAppAgentAPIError(WhatsAppAgentError):
|
||||
self.error = error
|
||||
|
||||
|
||||
def classify_error(status_code: int, payload: Any) -> APIError:
|
||||
def classify_error(
|
||||
status_code: int,
|
||||
payload: Any,
|
||||
*,
|
||||
media_endpoint: bool = False,
|
||||
) -> APIError:
|
||||
"""Extract the structured error from a non-2xx response body."""
|
||||
body = _as_dict(payload) or {}
|
||||
error = _as_dict(body.get("error")) or {}
|
||||
@@ -251,6 +265,7 @@ def classify_error(status_code: int, payload: Any) -> APIError:
|
||||
message=_text(error.get("message")),
|
||||
details=details,
|
||||
fbtrace_id=_text(error.get("fbtrace_id")),
|
||||
media_endpoint=media_endpoint,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -280,7 +280,9 @@ class WhatsAppAgentChannel(BaseChannel):
|
||||
)
|
||||
if 200 <= status_code < 300:
|
||||
return payload
|
||||
error = classify_error(status_code, payload)
|
||||
error = classify_error(
|
||||
status_code, payload, media_endpoint=method_key == "media"
|
||||
)
|
||||
if error.invalid_token:
|
||||
raise WhatsAppAgentAuthError(str(error))
|
||||
raise WhatsAppAgentAPIError(error)
|
||||
|
||||
@@ -10,8 +10,10 @@ explicit, crash-safe store on the channel side:
|
||||
* Marking a message read may delete it server-side, so a message must be
|
||||
recorded before its receipt is acknowledged.
|
||||
|
||||
The store keeps the cursor, the recently handled wamids, and accumulated
|
||||
contact display names in one atomically replaced JSON file.
|
||||
The store keeps the cursor, the recently handled wamids, the most recent
|
||||
creator identifier, and outbound delivery receipts in one atomically
|
||||
replaced JSON file. The API token is represented only by a SHA-256
|
||||
fingerprint, so the file carries no credential material.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -251,6 +251,24 @@ def test_only_confirmed_non_delivery_is_retryable():
|
||||
assert fatal.ambiguous is False
|
||||
|
||||
|
||||
def test_media_endpoint_400_is_not_treated_as_an_invalid_token():
|
||||
# Media methods share 400/100 between a bad token, an unknown or expired
|
||||
# media id, and a length cap, so an expired id must not stop the channel.
|
||||
expired = classify_error(
|
||||
400, {"error": {"code": 100, "message": "unknown media id"}}, media_endpoint=True
|
||||
)
|
||||
assert expired.invalid_token is False
|
||||
|
||||
# The same response on a non-media endpoint is the documented bad-token case.
|
||||
bad_token = classify_error(
|
||||
400, {"error": {"code": 100, "message": "(#100) invalid"}}, media_endpoint=False
|
||||
)
|
||||
assert bad_token.invalid_token is True
|
||||
|
||||
# A malformed header is 401 everywhere, including media.
|
||||
assert classify_error(401, {"error": {"code": 190}}, media_endpoint=True).invalid_token is True
|
||||
|
||||
|
||||
def test_duplicate_poller_is_reported_as_poll_replaced():
|
||||
error = classify_error(409, {"error": {"code": 1752041}})
|
||||
|
||||
|
||||
Reference in new issue
Block a user