Skip to content

fix: handle no gateway priority configured case#18

Open
Mrsandeep27 wants to merge 1 commit into
tata1mg:mainfrom
Mrsandeep27:fix/handle-empty-gateway-priority
Open

fix: handle no gateway priority configured case#18
Mrsandeep27 wants to merge 1 commit into
tata1mg:mainfrom
Mrsandeep27:fix/handle-empty-gateway-priority

Conversation

@Mrsandeep27
Copy link
Copy Markdown

Summary

Fixes #13 — when a channel has no gateway priority configured, the handler now logs and skips cleanly instead of raising ZeroDivisionError.

Problem

`PriorityGatewaySelection.select_gateway` computes:
```python
total_gateways = min(len(cls.get_default_priority()), len(current_priority))
return current_priority[n_attempts % total_gateways]
```
If both `default_priority` and `current_priority` are empty (channel configured with no priorities, or dynamic-priority expression evaluated to `[]`), `total_gateways` is `0` and the modulo raises `ZeroDivisionError`.

Additionally, the existing code silently swallows errors from `eval(priority_logic_expression)` — the comment says "Log the exception and fallback to default priority order" but the `except` block is just `pass`.

Fix

  1. Introduce `NoGatewayPriorityConfigured` exception. `select_gateway` raises it when both evaluated and default priorities are empty.
  2. Log the eval failure (previously silenced).
  3. In `AbstractHandler.notify`:
    • Short-circuit if `ENABLED_GATEWAYS_COUNT` is 0.
    • Catch `NoGatewayPriorityConfigured` and log the channel instead of crashing.

Test plan

  • Channel with empty `default_priority` and no `dynamic_priority` → logs `No gateway priority configured for channel `, no crash, `notify` returns `(None, None)`.
  • Channel with valid `default_priority` → behaves exactly as before.
  • Channel with malformed `dynamic_priority` → logs the eval failure, falls back to `default_priority`.
  • Channel with `ENABLED_GATEWAYS_COUNT` of 0 → early return with log, no call to `select_gateway`.

Previously, when a channel had no default_priority and no dynamic
priority (either unconfigured or an eval that returned []), the call
to select_gateway would:
  - silently swallow the eval error (just `pass`),
  - compute total_gateways = 0 and crash with ZeroDivisionError on
    `current_priority[n_attempts % 0]`.

Changes:
  - Raise a dedicated NoGatewayPriorityConfigured when both the
    evaluated and default priorities are empty, so callers can log
    and skip cleanly instead of hitting a ZeroDivisionError.
  - Log the eval failure (previously intentionally silenced) so
    misconfigured priority expressions surface in logs.
  - In AbstractHandler.notify, short-circuit when ENABLED_GATEWAYS_COUNT
    is 0 and catch NoGatewayPriorityConfigured, logging the channel
    and skipping the send instead of crashing.

Fixes tata1mg#13
@Mrsandeep27 Mrsandeep27 requested a review from a team as a code owner April 20, 2026 09:41
@deputydev-agent
Copy link
Copy Markdown

DeputyDev will no longer review pull requests automatically.To request a review, simply comment #review on your pull request—this will trigger an on-demand review whenever you need it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle no gateway priority case

1 participant