Permalink
Browse files

error handling and logging was added to JsonRpc.py

  • Loading branch information...
1 parent 5e46b35 commit 8136c5da542dbae4fd7330714dc274e3068ad893 unknown committed May 14, 2012
Showing with 49 additions and 33 deletions.
  1. +49 −33 lib/JsonRpc.py
View
@@ -55,13 +55,13 @@ def _getJsonFromBody(body):
return json_from_body
class JsonRpc(object):
- __slots__ = [ "request", "response", "jsonRequest", "requestHandler", "request", "response", "jsonrpc", "method", "params", "id", "result", "error"]
+ __slots__ = [ "request", "response", "jsonRequest", "requestHandler", "jsonrpc", "method", "params", "id", "result", "error", "log", "httpStatus"]
def __init__(self, request_handler):
self.requestHandler = request_handler
self.request = request_handler.request
self.response = request_handler.response
- self.error = {}
+ #self.error = {}
self._getJsonRequest()
if self.jsonRequest:
self.jsonrpc = self.jsonRequest.get("jsonrpc")
@@ -70,7 +70,6 @@ def __init__(self, request_handler):
self.id = self.jsonRequest.get("id")
if isinstance(self.id, list) and len(self.id) == 1:
self.id = self.id[0]
- #self.result = None
def _getArgumentDict(self):
d = {}
@@ -81,18 +80,42 @@ def _getArgumentDict(self):
d[a] = d[a][0]
return d
- def setErrorCode(self, error_code):
+ def redirect(self, url):
+ self.requestHandler.redirect(url)
+ assert self.response.status == 302
+ self.setHttpStatus(self.response.status)
+
+ def setHttpStatus(self, http_status):
+ assert isinstance(http_status, int)
+ assert not hasattr(self, "httpStatus")
+ self.httpStatus = http_status
+
+ def setError(self, error_code, error_message=None, error_data=None):
assert isinstance(error_code, int)
- assert isinstance(self.error, dict)
- self.error["code"] = error_code
- self.response.set_status(_toHttpStatus(error_code))
+ assert not hasattr(self, "error")
+ self.error = {
+ "code": error_code,
+ "message": error_message,
+ "data":error_data
+ }
+ http_status = _toHttpStatus(error_code)
+ self.setHttpStatus(http_status)
def setResultValule(self, key, value):
if not hasattr(self, "result"):
self.result = {}
assert isinstance(self.result, dict)
- assert self.result.get(key) is None
+ assert not self.result.has_key(key)
self.result[key] = value
+
+ def appendResultValue(self, key, value):
+ assert isinstance(self.result.has_key("key"), list)
+ self.result["key"].append(value)
+
+ def addLog(self, log_message):
+ if not hasattr(self, "log"):
+ self.log = []
+ self.log.append(log_message)
def setResult(self, result):
assert not hasattr(self, "result") or self.result is None
@@ -140,13 +163,6 @@ def getErrorCode(self):
pass
return None
- def setErrorMessage(self, error_message):
- assert isinstance(error_message, str)
- self.error["message"] = error_message
-
- def setErrorData(self, error_data):
- self.error["data"] = error_data
-
def _getJsonRequest(self):
"""getJsonRequest is intended to gather as many parameters as possible
even if parameters in URL or request body does not meet JSON-RPC 2.0 specification.
@@ -155,19 +171,14 @@ def _getJsonRequest(self):
if self.request.method == "POST" or self.request.method == "PUT":
# POST can change the state of servers.
# PUT should be idempotent.
- debug("POST or PUT")
try:
- debug("getting json from body")
self.jsonRequest = _getJsonFromBody(self.request.body)
- debug(self.jsonRequest)
return
except JSONDecodeError, e:
self.jsonRequest = None
- self.setErrorCode(JSON_RPC_ERROR_PARSE_ERROR)
- self.setErrorMessage(e.message)
+ self.setError(JSON_RPC_ERROR_PARSE_ERROR, e.message)
return
if self.request.method == "GET" or self.request.method == "DELETE":
- debug("GET or DELETE")
# GET should not change the state of servers.
# DELETE should be idempotent.
try:
@@ -177,35 +188,35 @@ def _getJsonRequest(self):
return
except JSONDecodeError, e:
self.jsonRequest = None
- self.setErrorCode(JSON_RPC_ERROR_PARSE_ERROR)
- self.setErrorMessage(e.message)
+ self.setError(JSON_RPC_ERROR_PARSE_ERROR, e.message)
return
self.jsonRequest = None
- self.setErrorCode(JSON_RPC_ERROR_INVALID_REQUEST)
- self.setErrorMessage("%s is unknown HTTP method" % self.request.method)
+ self.setError(JSON_RPC_ERROR_INVALID_REQUEST, "%s is unknown HTTP method" % self.request.method)
def write(self):
- if self.result is None:
+ if hasattr(self, "error"):
assert isinstance(self.error, dict)
+ assert not hasattr(self, "result")
self.response.content_type = "application/json"
- if self.getErrorCode() is None:
- self.setErrorCode(JSON_RPC_ERROR_INTERNAL_ERROR)
- self.setErrorMessage("No result without error code.")
params = {
"error" : self.error,
}
if hasattr(self, "id"):
params["id"] = self.id
-
+ if hasattr(self, "log"):
+ params["log"] = self.log
if hasattr(self, "jsonrpc") and self.jsonrpc == "2.0":
params["jsonrpc"] = self.jsonrpc
self.response.out.write(dumps(params))
return
+ # notification has no id
if not hasattr(self, "id") and hasattr(self, "method"):
- if self.getErrorCode() is None:
- self.response.set_status(204) # No content
+ assert not hasattr(self, "error")
+ assert not hasattr(self, "result")
+ assert not hasattr(self, "httpStatus")
+ self.response.set_status(204) # No content
return
if hasattr(self, "id") and hasattr(self, "method"):
@@ -214,14 +225,19 @@ def write(self):
}
if hasattr(self, "result"):
params["result"] = self.result
-
+ if hasattr(self, "log"):
+ params["log"] = self.log
if self.jsonrpc == "2.0":
params["jsonrpc"] = self.jsonrpc
cached_content = CachedContent(self.request.path, params)
cached_content.dump()
+ if hasattr(self, "httpStatus"):
+ self.response.set_status(self.httpStatus)
cached_content.write(self.requestHandler)
return
+ if hasattr(self, "log"):
+ self.result["log"] = self.log
cached_content = CachedContent(self.request.path, self.result)
cached_content.dump()
cached_content.write(self.requestHandler)

0 comments on commit 8136c5d

Please sign in to comment.