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:

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: RPCException

Indicates an error with an asset

type_code = 4
type_name = 'asset'
exception momotor.rpc.exception.AssetNotFoundException(text, wire_message=None)

Bases: RPCException

Indicates the asset does not exist

type_code = 5
type_name = 'asset_not_found'
exception momotor.rpc.exception.AuthException(text, wire_message=None)

Bases: RPCException

Indicates an authentication error

type_code = 2
type_name = 'auth'
exception momotor.rpc.exception.FormatException(text, wire_message=None)

Bases: RPCException

Indicates an invalid (combination of) arguments

type_code = 1
type_name = 'format'
exception momotor.rpc.exception.GRPCError(status, message=None, details=None)

Bases: Exception

Expected 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-status and grpc-message trailers. Possible values of the grpc-status trailer are described in the gRPC protocol definition. In grpclib these values are represented as Status enum.

Here are possible origins of this error:

  • you may raise this error to cancel current call on the server-side or return non-OK Status using send_trailing_metadata() method (e.g. resource not found)

  • server may return non-OK grpc-status in different failure conditions (e.g. invalid request)

  • client raises this error for non-OK grpc-status from the server

  • client may raise this error in different failure conditions (e.g. server returned unsupported :content-type header)

details

Error details

message

Error message

status

Status of the error

exception momotor.rpc.exception.JobException(text, wire_message=None)

Bases: RPCException

Indicates an error with the job

type_code = 3
type_name = 'job'
exception momotor.rpc.exception.ProtocolError

Bases: Exception

Unexpected error, raised by grpclib when your code violates gRPC protocol

This error means that you probably should fix your code.

exception momotor.rpc.exception.RPCException(text, wire_message=None)

Bases: Exception

Base class for all protocol exceptions.

When a subclass of RPCException is raised by an RPC method, the exception is caught and converted into a Message containing the exception’s type_code and text

Should not be raised directly, but use one of the subclasses.

Parameters:
  • text – textual explanation of the exception

  • wire_message – the Exception message to include in the RPC response

type_code = 0
type_name = 'rpc'
property wire_message
exception momotor.rpc.exception.StreamTerminatedError

Bases: Exception

Unexpected error, raised when we receive RST_STREAM frame from the other side

This 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 RPCException

Produces logging information on the momotor.rpc.exception logger

Parameters:
  • message (Message | None) – The message to processes

  • log_level – level to log the exception message on