Exception¶
Protocol messages carry errors as data instead of failing the call. A response message has an exception field. When the broker fills that field, the call itself succeeded at the transport level, but the request failed.
This module converts that field into a normal Python exception.
raise_message_exception() does nothing when
the field is empty. Otherwise it raises the exception class that matches
the error:
from momotor.rpc.exception import raise_message_exception
response = await stub.createJob(request)
raise_message_exception(response)
# The rest of this code only runs when the job was created.
All exception classes derive from
RPCException:
AuthExceptionfor authentication failures.JobExceptionfor job problems.AssetExceptionandAssetNotFoundExceptionfor asset problems.FormatExceptionfor malformed messages.
Catch the specific class when you can react to that problem. Catch
RPCException to handle every protocol
error in one place. The helper functions in this package, such as the ones
in Assets, already call
raise_message_exception() for you.
API reference¶
- exception momotor.rpc.exception.AssetException(text, wire_message=None)¶
Bases:
RPCExceptionIndicates an error with an asset
- type_code = 4¶
- type_name = 'asset'¶
- exception momotor.rpc.exception.AssetNotFoundException(text, wire_message=None)¶
Bases:
RPCExceptionIndicates the asset does not exist
- type_code = 5¶
- type_name = 'asset_not_found'¶
- exception momotor.rpc.exception.AuthException(text, wire_message=None)¶
Bases:
RPCExceptionIndicates an authentication error
- type_code = 2¶
- type_name = 'auth'¶
- exception momotor.rpc.exception.FormatException(text, wire_message=None)¶
Bases:
RPCExceptionIndicates an invalid (combination of) arguments
- type_code = 1¶
- type_name = 'format'¶
- exception momotor.rpc.exception.GRPCError(status, message=None, details=None)¶
Bases:
ExceptionExpected error, may be raised during RPC call
There can be multiple origins of this error. It can be generated on the server-side and on the client-side. If this error originates from the server, on the wire this error is represented as
grpc-statusandgrpc-messagetrailers. Possible values of thegrpc-statustrailer are described in the gRPC protocol definition. Ingrpclibthese values are represented asStatusenum.Here are possible origins of this error:
you may raise this error to cancel current call on the server-side or return non-OK
Statususingsend_trailing_metadata()method (e.g. resource not found)server may return non-OK
grpc-statusin different failure conditions (e.g. invalid request)client raises this error for non-OK
grpc-statusfrom the serverclient may raise this error in different failure conditions (e.g. server returned unsupported
:content-typeheader)
- details¶
Error details
- message¶
Error message
- exception momotor.rpc.exception.JobException(text, wire_message=None)¶
Bases:
RPCExceptionIndicates an error with the job
- type_code = 3¶
- type_name = 'job'¶
- exception momotor.rpc.exception.ProtocolError¶
Bases:
ExceptionUnexpected error, raised by
grpclibwhen your code violates gRPC protocolThis error means that you probably should fix your code.
- exception momotor.rpc.exception.RPCException(text, wire_message=None)¶
Bases:
ExceptionBase class for all protocol exceptions.
When a subclass of RPCException is raised by an RPC method, the exception is caught and converted into a
Messagecontaining the exception’s type_code and textShould not be raised directly, but use one of the subclasses.
- Parameters:
text – textual explanation of the exception
wire_message – the
Exceptionmessage to include in the RPC response
- type_code = 0¶
- type_name = 'rpc'¶
- property wire_message¶
- exception momotor.rpc.exception.StreamTerminatedError¶
Bases:
ExceptionUnexpected error, raised when we receive
RST_STREAMframe from the other sideThis error means that the other side decided to forcefully cancel current call, probably because of a protocol error.
- momotor.rpc.exception.raise_message_exception(message, *, log_level=10)¶
If the message has an exception field, convert it into an RPCException and raise it
If message is None, raises
UnexpectedEndOfStream.If message has an exception field set, raises a subclass of
RPCExceptionProduces logging information on the
momotor.rpc.exceptionlogger