Exception reference¶
-
exception
tinyrpc.exc.
BadReplyError
¶ Base class for all errors that caused processing of a reply to abort before it could be turned in a response object.
-
exception
tinyrpc.exc.
BadRequestError
¶ Base class for all errors that caused the processing of a request to abort before a request object could be instantiated.
-
error_respond
()¶ Create
RPCErrorResponse
to respond the error.Returns: A error responce instance or None
, if the protocol decides to drop the error silently.
-
-
exception
tinyrpc.exc.
InvalidReplyError
¶ A reply received was malformed (i.e. violated the specification) and could not be parsed into a response.
-
exception
tinyrpc.exc.
InvalidRequestError
¶ A request made was malformed (i.e. violated the specification) and could not be parsed.
-
exception
tinyrpc.exc.
MethodNotFoundError
¶ The desired method was not found.
-
exception
tinyrpc.exc.
RPCError
¶ Base class for all excetions thrown by
tinyrpc
.
-
exception
tinyrpc.exc.
ServerError
¶ An internal error in the RPC system occured.
Adding custom exceptions¶
Note
As per the specification you should use error codes -32000 to -32099 when adding server specific error messages. Error codes outside the range -32768 to -32000 are available for application specific error codes.
To add custom errors you need to combine an Exception
subclass
with the FixedErrorMessageMixin
class to create your exception
object which you can raise.
So a version of the reverse string example that dislikes palindromes could look like:
from tinyrpc.protocols.jsonrpc import FixedErrorMessageMixin, JSONRPCProtocol
from tinyrpc.dispatch import RPCDispatcher
dispatcher = RPCDispatcher()
class PalindromeError(FixedErrorMessageMixin, Exception):
jsonrpc_error_code = 99
message = "Ah, that's cheating!"
@dispatcher.public
def reverse_string(s):
r = s[::-1]
if r == s:
raise PalindromeError()
return r
Error with data¶
The specification states that the error
element of a reply may contain
an optional data
property. This property is now available for your use.
There are two ways that you can use to pass additional data with an Exception
.
It depends whether your application generates regular exceptions or exceptions derived
from FixedErrorMessageMixin
.
When using ordinary exceptions you normally pass a single parameter (an error message)
to the Exception
constructor.
By passing two parameters, the second parameter is assumed to be the data element.
@public
def fn():
raise Exception('error message', {'msg': 'structured data', 'lst': [1, 2, 3]})
This will produce the reply message:
{ "jsonrpc": "2.0",
"id": <some id>,
"error": {
"code": -32000,
"message": "error message",
"data": {"msg": "structured data", "lst": [1, 2, 3]}
}
}
When using FixedErrorMessageMixin
based exceptions the data is passed using
a keyword parameter.
class MyException(FixedErrorMessageMixin, Exception):
jsonrcp_error_code = 99
message = 'standard message'
@public
def fn():
raise MyException(data={'msg': 'structured data', 'lst': [1, 2, 3]})
This will produce the reply message:
{ "jsonrpc": "2.0",
"id": <some id>,
"error": {
"code": 99,
"message": "standard message",
"data": {"msg": "structured data", "lst": [1, 2, 3]}
}
}