1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-21 14:08:13 +00:00

refactor(core): get_context raises NoWireContext if not found

[no changelog]
This commit is contained in:
Martin Milata 2025-04-15 11:32:13 +02:00
parent 99f56a09d0
commit 26d66251a5
2 changed files with 17 additions and 7 deletions

View File

@ -10,16 +10,20 @@ if TYPE_CHECKING:
async def unpair(msg: BleUnpair) -> None: async def unpair(msg: BleUnpair) -> None:
from trezor.messages import Success from trezor.messages import Success
from trezor.ui.layouts import confirm_action 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: if msg.all:
await confirm_action("erase bonds", TR.ble__unpair_title, TR.ble__unpair_all) await confirm_action("erase bonds", TR.ble__unpair_title, TR.ble__unpair_all)
else: else:
await confirm_action("unpair", TR.ble__unpair_title, TR.ble__unpair_current) await confirm_action("unpair", TR.ble__unpair_title, TR.ble__unpair_current)
# NOTE: refactor into ctx.maybe_write if we end up doing this in multiple places
try:
ctx = get_context() ctx = get_context()
except NoWireContext:
await ctx.write(Success(message="Erasing..")) pass
else:
await ctx.write(Success(message="Erasing..."))
if msg.all: if msg.all:
ble.erase_bonds() ble.erase_bonds()

View File

@ -47,6 +47,10 @@ class UnexpectedMessageException(Exception):
self.msg = msg self.msg = msg
class NoWireContext(RuntimeError):
pass
CURRENT_CONTEXT: Context | None = None CURRENT_CONTEXT: Context | None = None
@ -58,7 +62,7 @@ async def call(
Raises if there is no context for this workflow.""" Raises if there is no context for this workflow."""
if CURRENT_CONTEXT is None: if CURRENT_CONTEXT is None:
raise RuntimeError("No wire context") raise NoWireContext
return await CURRENT_CONTEXT.call(msg, expected_type) 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.""" Raises if there is no context for this workflow."""
if CURRENT_CONTEXT is None: if CURRENT_CONTEXT is None:
raise RuntimeError("No wire context") raise NoWireContext
await CURRENT_CONTEXT.write(msg) await CURRENT_CONTEXT.write(msg)
del msg del msg
@ -101,9 +105,11 @@ def get_context() -> Context:
Result of this function should not be stored -- the context is technically allowed Result of this function should not be stored -- the context is technically allowed
to change inbetween any `await` statements. to change inbetween any `await` statements.
Raises KeyError if there is currently no context.
""" """
if CURRENT_CONTEXT is None: if CURRENT_CONTEXT is None:
raise RuntimeError("No wire context") raise NoWireContext
return CURRENT_CONTEXT return CURRENT_CONTEXT