ubii.framework.errors module¶
To get specific errors to raise for the different errors the master node can send during communication
with a client node this module defines some new Exception types that also inherit from ubii.proto.Error
and rules for the proto.marshal.Marshal for serialization of these Exceptions on the fly.
Importing this module registers the rules for the marshal of the ubii.proto module to convert between
the Error protobuf messages (internally used by the ubii.proto.Error wrapper) and the new UbiiError
exception type i.e. when this module is imported Error protobuf messages will not be converted to plain
ubii.proto.Error wrapper objects, but instead to UbiiError objects so that they can be used in a
The raise statement expression. Depending on e.g. the value if the ubii.proto.Error.title subtypes of
UbiiError might be used, i.e. it’s possible to differentiate the type of error in an The try statement statement.
Example
A short overview of the proto-plus marshaling process (since it’s not documented in detail):
Get the marshal name used in your proto-plus module, in this case
import ubii.proto marshal_name = ubii.proto.__protobuf__.marshalGet a reference to the marshal by name (same name returns same object, see documentation of
proto.marshal.Marshal)import proto.marshal marshal = proto.marshal.Marshal(name=marshal_name)Create a rule – you can use the existing rules of the proto-plus package as a starting point
from proto.marshal.rules.message import MessageRule class CustomRule(MessageRule): def to_proto(self, value): # convert wrapper to actual protobuf message type and return it ... def to_python(self, value, *, absent: bool | None = None): # convert actual protobuf message to python wrapper and return it, ...Register the rule at the Marshal you retrieved earlier. To do that you need the actual protobuf type that you want to handle with the rule, if you only got proto-plus wrappers at your disposal, you need to first get the internal type from the wrapper
Error_pb = ubii.proto.Error.pb() # actual protobuf type created for the Error wrapper marshal.register(Error_pb, CustomRule(descriptor=Error_pb, wrapper=ubii.proto.Error))
See also
proto.marshal.Marshal – documentation of the Marshal class in the proto-plus module.
ubii.proto.__protobuf__ – module level variable of the ubii-msg-formats module to expose the proto-plus
marshal and message pool
- exception ubii.framework.errors.UbiiError(mapping=None, *, ignore_unknown_fields=False, **kwargs)¶
-
Base class for all custom errors.
- classmethod rule() MessageRule¶
Fallback rule to convert between
ubii.proto.Errorand this type
- exception ubii.framework.errors.SessionRuntimeStopServiceError(mapping=None, *, ignore_unknown_fields=False, **kwargs)¶
Bases:
UbiiErrorRaise if a service request to stop a
ubii.proto.Sessionis unsuccessful.
- class ubii.framework.errors.ErrorRule(descriptor: type, wrapper: type)¶
Bases:
MessageRuleCustom MessageRule to convert Error protobuf messages to
UbiiErrororSessionRuntimeStopServiceErrorexceptions.- to_python(value, *, absent: bool | None = None)¶
If the
ubii.proto.Error.titlestarts with'SessionRuntimeStopService'the message is converted to aSessionRuntimeStopServiceError, otherwise a normalUbiiErroris created.
- to_proto(value)¶
Converts back to a protobuf message using the MessageRule implementation from the proto-plus package