Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

RPC Exceptions

Project Description

Simple, transparent, name-spaced exceptions across JSON-RPC services.


The rpc_exceptions project lives on github, and is available via pip.

Installing v0.1 From Pip

sudo pip install rpc_exceptions==0.1

Installing v0.1 From Source

curl | tar vzxf -
cd rpc_exceptions
sudo python install


Define a base exception with an api_name, exceptions for that API, and instantiate RPCExceptionHandler with those exceptions:

from rpc_exceptions import RPCExceptionHandler, WrappedRPCError

class TestError(WrappedRPCError):
    api_name = 'test'

class PEBCAKError(TestError):
    code = -1
    _default_message = 'Problem exists between chair and keyboard'

class FUBARError(TestError):
    code = -2
    _default_message = 'FUBARed'

error_handler = RPCExceptionHandler([ PEBCAKError, FUBARError ])

Decorate exposed functions with the instantiated RPCExceptionHandler’s wrap_rpc_exception decorator:

from wrapped_rpc.exceptions import error_handler, PEBCAKError, FUBARError
def fail1(arg):
    raise PEBCAKError

def fail2(arg):
    raise FUBARError

Coerce error codes and messages to exceptions the instantiated RPCExceptionHandler’s get_exception_instance method:

#client using tinyrpc
from wrapped_rpc.exceptions import error_handler, PEBCAKError, FUBARError
from tinyrpc import RPCClient, RPCError
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.http import HttpPostClientTransport

class WrappedRPCClient(RPCClient):
    def __init__(self, protocol, transport, api_name):
        self.protocol = protocol
        self.transport = transport
        self.api_name = api_name

    def _send_and_handle_reply(self, req):
        reply = self.transport.send_message(req.serialize())
        response = self.protocol.parse_reply(reply)
        if hasattr(response, 'error'):
                raise error_handler.get_exception_instance(
                         self.api_name, response.error)
            except KeyError:
                raise RPCError('Error calling remote procedure: %s' %\
        return response

rpc_client = WrappedRPCClient(
server = rpc_client.get_proxy()

except PEBCAKError as e:
    print 'code: %s, message: %s' % (e.code, e.msg)

except FUBARError as e:
    print 'code: %s, message: %s' % (e.code, e.msg)



Release History

Release History

This version
History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
rpc_exceptions-0.1.tar.gz (3.8 kB) Copy SHA256 Checksum SHA256 Source Apr 29, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting