mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-08 14:31:06 +00:00
feat(core): convert protobuf decoding errors to DataErrors
This commit is contained in:
parent
6ba08526e1
commit
b0a2297b14
@ -40,7 +40,7 @@ from trezor import log, loop, messages, ui, utils, workflow
|
|||||||
from trezor.messages import FailureType
|
from trezor.messages import FailureType
|
||||||
from trezor.messages.Failure import Failure
|
from trezor.messages.Failure import Failure
|
||||||
from trezor.wire import codec_v1
|
from trezor.wire import codec_v1
|
||||||
from trezor.wire.errors import ActionCancelled, Error
|
from trezor.wire.errors import ActionCancelled, DataError, Error
|
||||||
|
|
||||||
# Import all errors into namespace, so that `wire.Error` is available from
|
# Import all errors into namespace, so that `wire.Error` is available from
|
||||||
# other packages.
|
# other packages.
|
||||||
@ -117,6 +117,20 @@ if False:
|
|||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def _wrap_protobuf_load(
|
||||||
|
reader: protobuf.Reader,
|
||||||
|
expected_type: Type[protobuf.LoadedMessageType],
|
||||||
|
field_cache: protobuf.FieldCache = None,
|
||||||
|
) -> protobuf.LoadedMessageType:
|
||||||
|
try:
|
||||||
|
return protobuf.load_message(reader, expected_type, field_cache)
|
||||||
|
except Exception as e:
|
||||||
|
if e.args:
|
||||||
|
raise DataError("Failed to decode message: {}".format(e.args[0]))
|
||||||
|
else:
|
||||||
|
raise DataError("Failed to decode message")
|
||||||
|
|
||||||
|
|
||||||
class DummyContext:
|
class DummyContext:
|
||||||
async def call(self, *argv: Any) -> None:
|
async def call(self, *argv: Any) -> None:
|
||||||
pass
|
pass
|
||||||
@ -201,8 +215,7 @@ class Context:
|
|||||||
workflow.idle_timer.touch()
|
workflow.idle_timer.touch()
|
||||||
|
|
||||||
# look up the protobuf class and parse the message
|
# look up the protobuf class and parse the message
|
||||||
pbtype = messages.get_type(msg.type)
|
return _wrap_protobuf_load(msg.data, expected_type, field_cache)
|
||||||
return protobuf.load_message(msg.data, pbtype, field_cache) # type: ignore
|
|
||||||
|
|
||||||
async def read_any(
|
async def read_any(
|
||||||
self, expected_wire_types: Iterable[int]
|
self, expected_wire_types: Iterable[int]
|
||||||
@ -235,7 +248,7 @@ class Context:
|
|||||||
workflow.idle_timer.touch()
|
workflow.idle_timer.touch()
|
||||||
|
|
||||||
# parse the message and return it
|
# parse the message and return it
|
||||||
return protobuf.load_message(msg.data, exptype)
|
return _wrap_protobuf_load(msg.data, exptype)
|
||||||
|
|
||||||
async def write(
|
async def write(
|
||||||
self, msg: protobuf.MessageType, field_cache: protobuf.FieldCache = None
|
self, msg: protobuf.MessageType, field_cache: protobuf.FieldCache = None
|
||||||
@ -353,7 +366,7 @@ async def handle_session(
|
|||||||
|
|
||||||
# Try to decode the message according to schema from
|
# Try to decode the message according to schema from
|
||||||
# `req_type`. Raises if the message is malformed.
|
# `req_type`. Raises if the message is malformed.
|
||||||
req_msg = protobuf.load_message(msg.data, req_type)
|
req_msg = _wrap_protobuf_load(msg.data, req_type)
|
||||||
|
|
||||||
# At this point, message reports are all processed and
|
# At this point, message reports are all processed and
|
||||||
# correctly parsed into `req_msg`.
|
# correctly parsed into `req_msg`.
|
||||||
|
Loading…
Reference in New Issue
Block a user