Error handling

RPC Error codes and pre-defined exceptions

django-modern-rpc provide exceptions to cover common errors when requests are processed.

Error handling is fully described in both XML & JSON-RPC standards. Each common error have an associated faultCode and the response format is described, so errors can be handled correctly on the client side.

In django-modern-rpc, all errors are reported using a set of pre-defined exceptions. Thus, in JSON and XML-RPC handlers, when an exception is caught, the correct error response is returned to the view and transmitted to the client.

This simplify error management, and allow developers to simply return errors to clients from inside a RPC Method. The error codes values are defined in:

Pre-defined exceptions uses the following error codes:


# Used as minimal value for any custom error returned by the server
# Used as maximal value for any custom error returned by the server
exception modernrpc.exceptions.RPCException(code, message)[source]

This is the base class of all RPC exception. Custom exceptions raised by your RPC methods should inherits from RPCException.

exception modernrpc.exceptions.RPCInternalError(message)[source]

Raised by handlers if any standard exception is raised during the execution of the RPC method.

exception modernrpc.exceptions.RPCInvalidParams(message='')[source]

Raised by handlers if the RPC method’s params does not match the parameters in RPC request

exception modernrpc.exceptions.RPCInvalidRequest(message='')[source]

Raised by handlers if incoming JSON or XML data is not a valid JSON-RPC or XML-RPC data.

exception modernrpc.exceptions.RPCParseError(message='')[source]

Raised by handlers if the request can’t be read as valid JSOn or XML data.

exception modernrpc.exceptions.RPCUnknownMethod(name)[source]

Raised by handlers the RPC method called is not defined for the current entry point and protocol.

Customize error handling

If you want to define customized exceptions for your application, you can create RPCException sub-classes and set, for each custom exception, a faultCode to RPC_CUSTOM_ERROR_BASE + N with N a unique number.

Here is an example:

class MyException1(RPCException):
    def __init__(self, message):
        super(MyException1, self).__init__(RPC_CUSTOM_ERROR_BASE + 1, message)

class MyException2(RPCException):
    def __init__(self, message):
        super(MyException2, self).__init__(RPC_CUSTOM_ERROR_BASE + 2, message)

Anyway, any exception raised during the RPC method execution will generate a RPCInternalError with an error message constructed from the underlying error. As a result, the RPC client will have a correct message describing what went wrong.