def wrapper(*args, **kwargs): request = cherrypy.serving.request if request.handler is None: # reference to json_out of cherrypy. return function(*args, **kwargs) cherrypy.serving.response.headers['Content-Type'] = 'application/json' return json_encode(function(*args, **kwargs))
def errorHandler(status, message, traceback, version): """ Error handler for services. This handler will format the error response in a suitable transmission format (JSON/YAML/etc) based on what the client can handle. In addition, if the cherrypy.response object contains an attribute called B{errorInfo}, the value of this attribute will be added to the error response dictionary with the key B{errorInfo}. This will allow the calling process to include structured error information in an error response. @param status: The error status @param message: Any custom messages passed in @param traceback: If available @param version: The cherrypy version @see: U{http://cherrypy.readthedocs.org/en/latest/pkg/cherrypy.html?highlight=_cperror#module-cherrypy._cperror} """ # The status and message data to return dat = {'status': status, 'msg': message} # The caller may have added the errorInfo attribute to the response object. # If so, we add this as an additional error info key because this allows the # caller to return structured error info that will also be returned in the # marshalled output try: dat['errorInfo'] = cherrypy.response.errorInfo except AttributeError: # If it's not there, we just carry on pass # Only add the traceback if it is available if traceback: dat['tb'] = traceback # Serialise the data dat = json_encode(dat) # and set the correct content type for the returned data cherrypy.response.headers['Content-Type'] = 'application/json' # Return the serialized data return dat
def json_handler(*args, **kwargs): value = cherrypy.serving.request._json_inner_handler(*args, **kwargs) return json_encode(value)