Skip to content

api: Fail fast on UNAUTHORIZED and BAD_REQUEST in call_on_each_event. - #937

Open
SPY-Github22 wants to merge 1 commit into
zulip:mainfrom
SPY-Github22:fail-fast-unauthorized-bad-request
Open

SPY-Github22 wants to merge 1 commit into
zulip:mainfrom
SPY-Github22:fail-fast-unauthorized-bad-request

Conversation

@SPY-Github22

Copy link
Copy Markdown

Fixes #783.

Problem
Client.call_on_each_event aggressively retried on errors encountered during both initial queue registration (do_register) and long-polling event fetching (get_events). For unrecoverable client configuration errors—such as invalid/expired API keys (code="UNAUTHORIZED") or invalid narrow parameters (code="BAD_REQUEST")—this caused the client to silently loop forever without informing the caller of the error.

Solution

  1. In do_register(), check if res.get("code") in ("UNAUTHORIZED", "BAD_REQUEST") and immediately raise ZulipError(f"{code}: {msg}".strip(": ")).
  2. In the long-polling event loop, handle elif code in ("UNAUTHORIZED", "BAD_REQUEST"): directly within if "error" in res["result"]: (addressing earlier maintainer feedback from @timabbott in api: Fail fast on UNAUTHORIZED/BAD_REQUEST in call_on_each_event. (Fixes #783) #882).
  3. Preserves existing retry logic for transient errors (e.g. BAD_EVENT_QUEUE_ID, HTTP errors from server restarts, network disconnects).
  4. Added comprehensive unit tests in zulip/tests/test_call_on_each_event.py covering:
    • Immediate failure on registration error (UNAUTHORIZED and BAD_REQUEST).
    • Clean message formatting when msg is omitted or empty.
    • Immediate failure on event retrieval error (UNAUTHORIZED and BAD_REQUEST).
    • Recovery on transient BAD_EVENT_QUEUE_ID error.
    • Verification that call_on_each_message also fails fast as expected.

When call_on_each_event encounters errors during registration or
event fetching, it retried indefinitely even for unrecoverable
client errors such as invalid/expired API keys (UNAUTHORIZED) or
malformed query arguments (BAD_REQUEST). This led to silent infinite
retry loops.

Fail fast by immediately raising ZulipError when the server returns
code UNAUTHORIZED or BAD_REQUEST in both do_register and the main
event loop.

Fixes zulip#783.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python REST API: Fail fast if code is either "UNAUTHORIZED" or "BAD_REQUEST"

2 participants