diff --git a/core/src/apps/management/ble/unpair.py b/core/src/apps/management/ble/unpair.py index 33c9ac086f..0fe9873f24 100644 --- a/core/src/apps/management/ble/unpair.py +++ b/core/src/apps/management/ble/unpair.py @@ -10,16 +10,20 @@ if TYPE_CHECKING: async def unpair(msg: BleUnpair) -> None: from trezor.messages import Success from trezor.ui.layouts import confirm_action - from trezor.wire.context import get_context + from trezor.wire.context import NoWireContext, get_context if msg.all: await confirm_action("erase bonds", TR.ble__unpair_title, TR.ble__unpair_all) else: await confirm_action("unpair", TR.ble__unpair_title, TR.ble__unpair_current) - ctx = get_context() - - await ctx.write(Success(message="Erasing..")) + # NOTE: refactor into ctx.maybe_write if we end up doing this in multiple places + try: + ctx = get_context() + except NoWireContext: + pass + else: + await ctx.write(Success(message="Erasing...")) if msg.all: ble.erase_bonds() diff --git a/core/src/trezor/wire/context.py b/core/src/trezor/wire/context.py index 56df34fbc5..1f463bc540 100644 --- a/core/src/trezor/wire/context.py +++ b/core/src/trezor/wire/context.py @@ -47,6 +47,10 @@ class UnexpectedMessageException(Exception): self.msg = msg +class NoWireContext(RuntimeError): + pass + + CURRENT_CONTEXT: Context | None = None @@ -58,7 +62,7 @@ async def call( Raises if there is no context for this workflow.""" if CURRENT_CONTEXT is None: - raise RuntimeError("No wire context") + raise NoWireContext return await CURRENT_CONTEXT.call(msg, expected_type) @@ -72,7 +76,7 @@ async def call_any( Raises if there is no context for this workflow.""" if CURRENT_CONTEXT is None: - raise RuntimeError("No wire context") + raise NoWireContext await CURRENT_CONTEXT.write(msg) del msg @@ -101,9 +105,11 @@ def get_context() -> Context: Result of this function should not be stored -- the context is technically allowed to change inbetween any `await` statements. + + Raises KeyError if there is currently no context. """ if CURRENT_CONTEXT is None: - raise RuntimeError("No wire context") + raise NoWireContext return CURRENT_CONTEXT