Skip to content

Commit

Permalink
Merge pull request #1270 from Bastian-Krause/bst/improve-target-excep…
Browse files Browse the repository at this point in the history
…tions

target: improve exceptions
  • Loading branch information
Bastian-Krause committed Nov 10, 2023
2 parents 1f416df + ca2b88d commit 7e74c8c
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions labgrid/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ def get_resource(self, cls, *, name=None, wait_avail=True):
name_msg = f" named '{name}'" if name else ""
if other_names:
raise NoResourceFoundError(
f"no {cls} resource{name_msg} found in {self}, matching resources with other names: {other_names}" # pylint: disable=line-too-long
f"no {cls.__name__} resource{name_msg} found in {self}, matching resources with other names: {other_names}" # pylint: disable=line-too-long
)

raise NoResourceFoundError(
f"no {cls} resource{name_msg} found in {self}"
f"no {cls.__name__} resource{name_msg} found in {self}"
)
elif len(found) > 1:
raise NoResourceFoundError(
f"multiple resources matching {cls} found in {self}", found=found
f"multiple resources matching {cls.__name__} found in {self}", found=found
)
if wait_avail:
self.await_resources(found)
Expand Down Expand Up @@ -178,12 +178,12 @@ def _get_driver(self, cls, *, name=None, resource=None, activate=True, active=Fa
if other_names:
raise NoDriverFoundError(
"no {active}{cls} driver{name} found in {target}, matching resources with other names: {other_names}".format( # pylint: disable=line-too-long
active="active " if active else "", cls=cls, name=name_msg, target=self,
other_names=other_names)
active="active " if active else "", cls=cls.__name__, name=name_msg,
target=self, other_names=other_names)
)

raise NoDriverFoundError(
f"no {'active ' if active else ''}{cls} driver{name_msg} found in {self}"
f"no {'active ' if active else ''}{cls.__name__} driver{name_msg} found in {self}"
)
elif len(found) > 1:
prio_last = -255
Expand All @@ -202,7 +202,7 @@ def _get_driver(self, cls, *, name=None, resource=None, activate=True, active=Fa
else:
raise NoDriverFoundError(
"multiple {active}drivers matching {cls} found in {target} with the same priorities".format( # pylint: disable=line-too-long
active="active " if active else "", cls=cls, target=self)
active="active " if active else "", cls=cls.__name__, target=self)
)
if activate:
self.activate(found[0])
Expand Down Expand Up @@ -276,7 +276,7 @@ def __getitem__(self, key):
cls = target_factory.class_from_string(cls)
if not issubclass(cls, (Driver, abc.ABC)): # all Protocols derive from ABC
raise NoDriverFoundError(
f"invalid driver class {cls}"
f"invalid driver class {cls.__name__}"
)

return self.get_active_driver(cls, name=name)
Expand Down Expand Up @@ -370,13 +370,16 @@ def bind_driver(self, client):
except NoSupplierFoundError as e:
errors.append(e)
if not suppliers:
client_name = client.name or client.__class__.__name__
if optional:
supplier = None
elif len(errors) == 1:
raise errors[0]
err = errors[0]
err_cls = type(err)
raise err_cls(f"binding {client_name} failed: {err}") from err
else:
raise NoSupplierFoundError(
f"no supplier matching {requirements} found in {self} (errors: {errors})"
f"binding {client_name} failed: no supplier matching {requirements} found in {self} (errors: {errors})"
)
elif len(suppliers) > 1:
raise NoSupplierFoundError(f"conflicting suppliers matching {requirements} found in target {self}") # pylint: disable=line-too-long
Expand Down

0 comments on commit 7e74c8c

Please sign in to comment.