Skip to main content

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

Installing

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 https://github.com/axialmarket/rpc_exceptions/achive/version_0.1.tar.gz | tar vzxf -
cd rpc_exceptions
sudo python setup.py install

Examples

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
@error_handler.wrap_rpc_exception
def fail1(arg):
    raise PEBCAKError

@error_handler.wrap_rpc_exception
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'):
            try:
                raise error_handler.get_exception_instance(
                         response._jsonrpc_error_code,
                         self.api_name, response.error)
            except KeyError:
                raise RPCError('Error calling remote procedure: %s' %\
                               response.error)
        return response

rpc_client = WrappedRPCClient(
    JSONRPCProtocol(),
    HttpPostClientTransport('http://localhost:1234'),
    'test'
)
server = rpc_client.get_proxy()

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

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

License

BSD, See LICENSE.txt

Release files for rpc_exceptions 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rpc_exceptions 0.1
File Size Uploaded
rpc_exceptions-0.1.tar.gz 3.8 kB Details

Release files / rpc_exceptions-0.1.tar.gz

Download URL rpc_exceptions-0.1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
eb40500c497014cccffc7fcbb1e274073eb10e8d79bf1cae62f65baed2114d63
BLAKE2b-256 checksum
How to use checksums
fa89cad1332eace9bf167cd13977aced8a4f7e486b9cd28fcc7a7ab711fb20e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page