jsonrpc-base

Pure python standard library JSONRPC data interface
git clone git://git.defalsify.org/python-jsonrpc-base.git
Log | Files | Refs | LICENSE

error.py (2627B)


      1 # local imports
      2 from .base import JSONRPCBase
      3 
      4 
      5 class JSONRPCException(Exception, JSONRPCBase):
      6     message = 'Unknown'
      7 
      8     def __init__(self, v, request_id=None):
      9        context_v = '{} error'.format(self.message)
     10        if v != None:
     11            context_v += ': ' + v
     12 
     13        self.request_id = request_id
     14 
     15        super(JSONRPCException, self).__init__(context_v)
     16 
     17     # Thanks to https://stackoverflow.com/questions/35282222/in-python-how-do-i-cast-a-class-object-to-a-dict
     18     def __iter__(self):
     19         if self.request_id == None:
     20             raise AttributeError('request id cannot be undefined when serializing error')
     21         yield 'jsonrpc', JSONRPCBase.version_string
     22         yield 'id', self.request_id
     23         yield 'error', {
     24                 'code': self.code,
     25                 'message': str(self),
     26                 }
     27 
     28 
     29 class JSONRPCCustomException(JSONRPCException):
     30     code = -32000
     31     message = 'Server'
     32 
     33 
     34 class JSONRPCParseError(JSONRPCException):
     35     code = -32700
     36     message = 'Parse'
     37 
     38 
     39 class JSONRPCInvalidRequestError(JSONRPCException):
     40     code = -32600
     41     message = 'Invalid request'
     42 
     43 
     44 class JSONRPCMethodNotFoundError(JSONRPCException):
     45     code = -32601
     46     message = 'Method not found'
     47 
     48 
     49 class JSONRPCInvalidParametersError(JSONRPCException):
     50     code = -32602
     51     message = 'Invalid parameters'
     52 
     53 
     54 class JSONRPCInternalError(JSONRPCException):
     55     code = -32603
     56     message = 'Internal'
     57 
     58 
     59 class JSONRPCUnhandledErrorException(KeyError):
     60     pass
     61 
     62 
     63 class JSONRPCErrors:
     64     reserved_max = -31999
     65     reserved_min = -32768
     66     local_max = -32000
     67     local_min = -32099
     68 
     69     translations = {
     70         -32700: JSONRPCParseError,
     71         -32600: JSONRPCInvalidRequestError,
     72         -32601: JSONRPCMethodNotFoundError,
     73         -32602: JSONRPCInvalidParametersError,
     74         -32603: JSONRPCInternalError,
     75             }
     76 
     77     @classmethod
     78     def add(self, code, exception_object):
     79         if code < self.local_min or code > self.local_max:
     80             raise ValueError('code must be in range <{},{}>'.format(self.local_min, self.local_max))
     81         exc = self.translations.get(code)
     82         if exc != None:
     83             raise ValueError('code already registered with {}'.format(exc))
     84 
     85         if not issubclass(exception_object, JSONRPCCustomException):
     86             raise ValueError('exception object must be a subclass of jsonrpc_base.error.JSONRPCCustomException')
     87 
     88         self.translations[code] = exception_object
     89 
     90 
     91     @classmethod
     92     def get(self, code, v=None):
     93         e = self.translations.get(code)
     94         if e == None:
     95             raise JSONRPCUnhandledErrorException(code)
     96         return e(v)