Skip to content

Commit

Permalink
Improve error for unknown id references
Browse files Browse the repository at this point in the history
  • Loading branch information
bodograumann committed Apr 3, 2024
1 parent 407ee9d commit 60767c6
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions holper/iof/importer.py
Expand Up @@ -11,6 +11,18 @@
EntityWithId = TypeVar("EntityWithId", bound=model.HasExternalIds)


class NoSuchEntityError(Exception):
def __init__(self, issuer: str, external_id: str, scope: set[str]) -> None:
super().__init__(
f"Unable to find entity in list with id {issuer} and issuer {external_id}\n"
"Possible values: "
+ (", ".join(scope)),
)
self.issuer = issuer
self.external_id = external_id
self.scope = scope


class IdArgs(TypedDict):
issuer: str
external_id: str
Expand All @@ -30,14 +42,25 @@ def extract_id(self, id_: Id) -> IdArgs:

def find_by_id(self, entities: list[EntityWithId], id_: Id) -> EntityWithId:
args = self.extract_id(id_)
return next(
entity
for entity in entities
if any(
entity_id.issuer == args["issuer"] and entity_id.external_id == args["external_id"]
for entity_id in entity.external_ids
try:
return next(
entity
for entity in entities
if any(
entity_id.issuer == args["issuer"] and entity_id.external_id == args["external_id"]
for entity_id in entity.external_ids
)
)
)
except StopIteration as error:
raise NoSuchEntityError(
args["issuer"],
args["external_id"],
{
f"{entity_id.issuer}/{entity_id.external_id}"
for entity in entities
for entity_id in entity.external_ids
},
) from error

def find_country(self, ioc_code: str) -> model.Country | None:
try:
Expand Down Expand Up @@ -143,7 +166,7 @@ def import_organisation(self, organisation: Organisation | None) -> model.Organi
if organisation is None:
return None
if organisation.id is not None:
with suppress(StopIteration):
with suppress(NoSuchEntityError):
return self.find_by_id(self.organisations, organisation.id)

imported = model.Organisation(
Expand Down

0 comments on commit 60767c6

Please sign in to comment.