Exemplo n.º 1
0
    def finish(self, value, request):
        request.setHeader("Content-Type", "application/json; charset=UTF-8")
        if isinstance(value, Failure):
            err = value.value
            if self.debug:
                print "-" * 30, "TRACEKBACK", "-" * 30
                value.printTraceback()
                print "^" * 30, "TRACEKBACK", "^" * 30
            request.setResponseCode(500)
            if isinstance(err, backend.ValidationError):
                request.setResponseCode(400)
            elif isinstance(err, backend.NodeNotFound):
                request.setResponseCode(404)
            elif isinstance(err, backend.NodeInUseError):
                request.setResponseCode(400)
            elif isinstance(err, backend.EmptyInputData):
                request.setResponseCode(400)
            elif isinstance(err, backend.BatchOperationError):
                request.setResponseCode(400)
            elif isinstance(err, Exception) and not isinstance(err, backend.GenericError):
                err = dict(error="UnknownError", message=err.message)
            request.write(json_encode(dict(err)) + "\n")
        else:
            request.setResponseCode(200)
            request.write(json_encode(value) + "\n")

        info("respone time: %.3fms" % ((time.time() - self.startTime) * 1000))
        request.finish()
Exemplo n.º 2
0
    def finish(self, value, request):
        request.setHeader('Content-Type', 'application/json; charset=UTF-8')
        if isinstance(value, Failure):
            err = value.value
            request.setResponseCode(500)
            error = dict(error="generic", message=str(err))
            request.write(json_encode(error) + "\n")
        else:
            request.setResponseCode(200)
            request.write(json_encode(value) + "\n")

        info("respone time: %.3fms" % ((time.time() - self.startTime) * 1000))
        request.finish()