1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
""" JSON-RPC Exceptions."""
from . import six
import json
class JSONRPCError(object):
""" Error for JSON-RPC communication.
When a rpc call encounters an error, the Response Object MUST contain the
error member with a value that is a Object with the following members:
Parameters
----------
code: int
A Number that indicates the error type that occurred.
This MUST be an integer.
The error codes from and including -32768 to -32000 are reserved for
pre-defined errors. Any code within this range, but not defined
explicitly below is reserved for future use. The error codes are nearly
the same as those suggested for XML-RPC at the following
url: http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
message: str
A String providing a short description of the error.
The message SHOULD be limited to a concise single sentence.
data: int or str or dict or list, optional
A Primitive or Structured value that contains additional
information about the error.
This may be omitted.
The value of this member is defined by the Server (e.g. detailed error
information, nested errors etc.).
"""
serialize = staticmethod(json.dumps)
deserialize = staticmethod(json.loads)
def __init__(self, code=None, message=None, data=None):
self._data = dict()
self.code = getattr(self.__class__, "CODE", code)
self.message = getattr(self.__class__, "MESSAGE", message)
self.data = data
def __get_code(self):
return self._data["code"]
def __set_code(self, value):
if not isinstance(value, six.integer_types):
raise ValueError("Error code should be integer")
self._data["code"] = value
code = property(__get_code, __set_code)
def __get_message(self):
return self._data["message"]
def __set_message(self, value):
if not isinstance(value, six.string_types):
raise ValueError("Error message should be string")
self._data["message"] = value
message = property(__get_message, __set_message)
def __get_data(self):
return self._data.get("data")
def __set_data(self, value):
if value is not None:
self._data["data"] = value
data = property(__get_data, __set_data)
@classmethod
def from_json(cls, json_str):
data = cls.deserialize(json_str)
return cls(
code=data["code"], message=data["message"], data=data.get("data"))
@property
def json(self):
return self.serialize(self._data)
class JSONRPCParseError(JSONRPCError):
""" Parse Error.
Invalid JSON was received by the server.
An error occurred on the server while parsing the JSON text.
"""
CODE = -32700
MESSAGE = "Parse error"
class JSONRPCInvalidRequest(JSONRPCError):
""" Invalid Request.
The JSON sent is not a valid Request object.
"""
CODE = -32600
MESSAGE = "Invalid Request"
class JSONRPCMethodNotFound(JSONRPCError):
""" Method not found.
The method does not exist / is not available.
"""
CODE = -32601
MESSAGE = "Method not found"
class JSONRPCInvalidParams(JSONRPCError):
""" Invalid params.
Invalid method parameter(s).
"""
CODE = -32602
MESSAGE = "Invalid params"
class JSONRPCInternalError(JSONRPCError):
""" Internal error.
Internal JSON-RPC error.
"""
CODE = -32603
MESSAGE = "Internal error"
class JSONRPCServerError(JSONRPCError):
""" Server error.
Reserved for implementation-defined server-errors.
"""
CODE = -32000
MESSAGE = "Server error"
class JSONRPCException(Exception):
""" JSON-RPC Exception."""
pass
class JSONRPCInvalidRequestException(JSONRPCException):
""" Request is not valid."""
pass
class JSONRPCDispatchException(JSONRPCException):
""" JSON-RPC Dispatch Exception.
Should be thrown in dispatch methods.
"""
def __init__(self, code=None, message=None, data=None, *args, **kwargs):
super(JSONRPCDispatchException, self).__init__(args, kwargs)
self.error = JSONRPCError(code=code, data=data, message=message)
|