Skip to content

fix(analyzers): handle empty ASN history in BGPRanking#3733

Open
Valyrian-Code wants to merge 1 commit into
intelowlproject:developfrom
Valyrian-Code:fix/bgp-ranking-empty-response
Open

fix(analyzers): handle empty ASN history in BGPRanking#3733
Valyrian-Code wants to merge 1 commit into
intelowlproject:developfrom
Valyrian-Code:fix/bgp-ranking-empty-response

Conversation

@Valyrian-Code
Copy link
Copy Markdown

@Valyrian-Code Valyrian-Code commented Jun 1, 2026

Description

When BGP Ranking has no ASN history for an IP, the API returns an empty response object. The current implementation calls .popitem() on that empty dictionary, resulting in an uncaught:

KeyError: 'popitem(): dictionary is empty'

This occurs before the intended AnalyzerRunException("ASN not found ...") can be raised.

This change handles the empty-response case explicitly, allowing the analyzer to raise a clean AnalyzerRunException instead of terminating with a traceback. Behavior remains unchanged when ASN history is available.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Tests

Added a regression test, test_run_raises_clean_exception_on_empty_asn_history, which mocks an empty API response:

{"response": {}}

The test verifies that AnalyzerRunException is raised.

  • Fails on the current code with:

    KeyError: 'popitem(): dictionary is empty'
    
  • Passes with this fix applied.

All existing tests continue to pass. ruff check and ruff format --check are also clean.

Copilot AI review requested due to automatic review settings June 1, 2026 10:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Prevents BGPRanking.run() from crashing when the BGP Ranking API returns an empty ASN history, and adds a regression unit test for the scenario.

Changes:

  • Add guard logic in BGPRanking.run() to handle empty "response" without popitem() raising KeyError.
  • Add a unit test asserting a clean AnalyzerRunException is raised for empty ASN history.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tests/api_app/analyzers_manager/unit_tests/observable_analyzers/test_bgp_ranking.py Adds a regression test for empty ASN history behavior.
api_app/analyzers_manager/observable_analyzers/bgp_ranking.py Adds a defensive check to avoid popitem() on an empty dict and raise AnalyzerRunException instead.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread api_app/analyzers_manager/observable_analyzers/bgp_ranking.py
When BGP Ranking has no ASN history for an IP, the API returns an empty
"response" object. Calling .popitem() on that empty dict raised an
uncaught "KeyError: 'popitem(): dictionary is empty'" before the intended
"ASN not found" AnalyzerRunException could be raised.

Guard the empty case so the analyzer reports a clean AnalyzerRunException
instead of crashing with a traceback. Behaviour is unchanged when ASN
history is present. Adds a regression test covering the empty response.
@Valyrian-Code Valyrian-Code force-pushed the fix/bgp-ranking-empty-response branch from a6bf663 to 81008c1 Compare June 1, 2026 10:29
@Valyrian-Code
Copy link
Copy Markdown
Author

Hi @mlodic,

could you please review this PR and share any suggestions? Thanks!

GIF

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.

2 participants