mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-10 07:20:56 +00:00
chore(python): add session_context to cli to handle exceptions gracefully
[no changelog]
This commit is contained in:
parent
719d6de934
commit
811b00d3b4
@ -236,6 +236,55 @@ class TrezorConnection:
|
|||||||
raise click.ClickException(str(e)) from e
|
raise click.ClickException(str(e)) from e
|
||||||
# other exceptions may cause a traceback
|
# other exceptions may cause a traceback
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def session_context(
|
||||||
|
self,
|
||||||
|
empty_passphrase: bool = False,
|
||||||
|
derive_cardano: bool = False,
|
||||||
|
management: bool = False,
|
||||||
|
must_resume: bool = False,
|
||||||
|
):
|
||||||
|
"""Get a session instance as a context manager. Handle errors in a manner
|
||||||
|
appropriate for end-users.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
>>> with obj.session_context() as session:
|
||||||
|
>>> do_your_actions_here()
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if management:
|
||||||
|
session = self.get_management_session()
|
||||||
|
else:
|
||||||
|
session = self.get_session(
|
||||||
|
derive_cardano=derive_cardano,
|
||||||
|
empty_passphrase=empty_passphrase,
|
||||||
|
must_resume=must_resume,
|
||||||
|
)
|
||||||
|
except exceptions.DeviceLockedException:
|
||||||
|
click.echo(
|
||||||
|
"Device is locked, enter a pin on the device.",
|
||||||
|
err=True,
|
||||||
|
)
|
||||||
|
except transport.DeviceIsBusy:
|
||||||
|
click.echo("Device is in use by another process.")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception:
|
||||||
|
click.echo("Failed to find a Trezor device.")
|
||||||
|
if self.path is not None:
|
||||||
|
click.echo(f"Using path: {self.path}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
yield session
|
||||||
|
except exceptions.Cancelled:
|
||||||
|
# handle cancel action
|
||||||
|
click.echo("Action was cancelled.")
|
||||||
|
sys.exit(1)
|
||||||
|
except exceptions.TrezorException as e:
|
||||||
|
# handle any Trezor-sent exceptions as user-readable
|
||||||
|
raise click.ClickException(str(e)) from e
|
||||||
|
# other exceptions may cause a traceback
|
||||||
|
|
||||||
|
|
||||||
def with_session(
|
def with_session(
|
||||||
func: "t.Callable[Concatenate[Session, P], R]|None" = None,
|
func: "t.Callable[Concatenate[Session, P], R]|None" = None,
|
||||||
@ -262,26 +311,18 @@ def with_session(
|
|||||||
def function_with_session(
|
def function_with_session(
|
||||||
obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
||||||
) -> "R":
|
) -> "R":
|
||||||
try:
|
with obj.session_context(
|
||||||
if management:
|
empty_passphrase=empty_passphrase,
|
||||||
session = obj.get_management_session()
|
derive_cardano=derive_cardano,
|
||||||
else:
|
management=management,
|
||||||
# TODO try (sys.exit ve finally)
|
must_resume=must_resume,
|
||||||
session = obj.get_session(
|
) as session:
|
||||||
derive_cardano=derive_cardano,
|
try:
|
||||||
empty_passphrase=empty_passphrase,
|
return func(session, *args, **kwargs)
|
||||||
must_resume=must_resume,
|
|
||||||
)
|
|
||||||
|
|
||||||
return func(session, *args, **kwargs)
|
finally:
|
||||||
except exceptions.DeviceLockedException:
|
pass
|
||||||
click.echo(
|
# TODO try end session if not resumed
|
||||||
"Device is locked, enter a pin on the device.",
|
|
||||||
err=True,
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
pass
|
|
||||||
# TODO try end session if not resumed
|
|
||||||
|
|
||||||
return function_with_session
|
return function_with_session
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user