Skip to content

get_cre_by_db_id raises IndexError when get_CREs returns an empty list #861

@shiwani42

Description

@shiwani42

Issue

What is the issue?

get_cre_by_db_id() in application/database/db.py (line 1156) does:

return self.get_CREs(external_id=external_id[0])[0]

get_CREs() explicitly returns [] when no matching CRE is found (line 1276). Indexing into an empty list raises IndexError: list index out of range.

The function already handles the case where the CRE id is missing from the database (returning None), but not the case where the external_id exists in the CRE table but get_CREs finds nothing, which can happen under data inconsistency.

This function is called in __get_all_nodes_and_cres (iterates every CRE in the database) and get_path_between_cres. Both callers check if shallow_CRE expecting None, so they already handle None correctly but never receive it when get_CREs returns [].

Expected Behaviour

get_cre_by_db_id returns None and logs an error when get_CREs finds no results for the given external_id, consistent with its existing error handling.

Actual Behaviour

An unhandled IndexError is raised, which propagates as a 500 from any endpoint that triggers with_graph() or get_path_between_cres().

Steps to reproduce

Trigger any data inconsistency where a row exists in the CRE table but get_CREs() returns no results for its external_id, then call an endpoint that invokes __get_all_nodes_and_cres() or get_path_between_cres().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions