mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-20 21:48:14 +00:00
refactor(core): get_context raises NoWireContext if not found
[no changelog]
This commit is contained in:
parent
99f56a09d0
commit
26d66251a5
@ -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()
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user