Пример #1
0
    def send_error(self, status_code=500, reason="unknown", detail={}, **kwargs):
        """
        Sends an error response to the client, finishing the request in the
        process.

        If the client has requested an error redirect rather than a status
        code then the error is formatted into URL parameters and sent on the
        redirect instead.
        """
        redirect_url = self.get_argument("onfailure", None)
        if reason == "unknown" and "exception" in kwargs:
            e = kwargs["exception"]
            if isinstance(e, HTTPError):
                reason = e.log_message or "unknown"
        if redirect_url:
            # The client (likely a webpage) has requested that we signal
            # failure by redirecting to a specific URL.
            redirect_url = utils.append_url_params(redirect_url,
                                                   error="true",
                                                   status=status_code,
                                                   message=httplib.responses[status_code],
                                                   reason=reason,
                                                   detail=json.dumps(detail))
            self.redirect(redirect_url)
        else:
            # Handle normally.  this will loop back through write_error below.
            cyclone.web.RequestHandler.send_error(self,
                                                  status_code=status_code,
                                                  reason=reason,
                                                  detail=detail,
                                                  **kwargs)
Пример #2
0
 def test_append_url_params(self):
     self.assertEquals(append_url_params("foo", bar="baz"), "foo?bar=baz")
     self.assertEquals(append_url_params("foo?", bar="baz"), "foo?bar=baz")
     self.assertEquals(append_url_params("foo?bif=bop", bar="baz"),
                       "foo?bif=bop&bar=baz")
     self.assertEquals(append_url_params("foo?bif=bop&", bar="baz"),
                       "foo?bif=bop&bar=baz")
     self.assertEquals(append_url_params("foo?bif=bop&boz", bar="baz"),
                       "foo?bif=bop&boz&bar=baz")
     self.assertEquals(append_url_params("", bar="baz"), "?bar=baz")
     self.assertEquals(append_url_params("foo#bif", bar="baz"),
                       "foo?bar=baz#bif")
Пример #3
0
 def test_append_url_params(self):
     self.assertEquals(append_url_params("foo", bar="baz"),
                       "foo?bar=baz")
     self.assertEquals(append_url_params("foo?", bar="baz"),
                       "foo?bar=baz")
     self.assertEquals(append_url_params("foo?bif=bop", bar="baz"),
                       "foo?bif=bop&bar=baz")
     self.assertEquals(append_url_params("foo?bif=bop&", bar="baz"),
                       "foo?bif=bop&bar=baz")
     self.assertEquals(append_url_params("foo?bif=bop&boz", bar="baz"),
                       "foo?bif=bop&boz&bar=baz")
     self.assertEquals(append_url_params("", bar="baz"),
                       "?bar=baz")
     self.assertEquals(append_url_params("foo#bif", bar="baz"),
                       "foo?bar=baz#bif")
Пример #4
0
    def send_success(self, status_code=200, data={}):
        """
        If the client has requested a redirect response, serializes the data
        into a redirect.  Otherwise, equivalent to:

        self.set_status(status_code)
        self.finish(data)
        """
        redirect_url = self.get_argument("onsuccess", None)
        if redirect_url:
            redirect_url = utils.append_url_params(redirect_url,
                                                   success="true",
                                                   status=status_code,
                                                   message=httplib.responses[status_code],
                                                   data=json.dumps(data))
            self.redirect(redirect_url)
        else:
            self.set_status(status_code)
            self.finish(data)
Пример #5
0
    def send_error(self,
                   status_code=500,
                   reason="unknown",
                   detail={},
                   **kwargs):
        """
        Sends an error response to the client, finishing the request in the
        process.

        If the client has requested an error redirect rather than a status
        code then the error is formatted into URL parameters and sent on the
        redirect instead.
        """
        redirect_url = self.get_argument("onfailure", None)
        headers = {}
        if "exception" in kwargs:  # pragma: no cover
            e = kwargs["exception"]
            if reason == "unknown" and isinstance(e, HTTPError):
                reason = e.log_message or "unknown"
            if isinstance(e, HTTPErrorEx):
                headers = e.headers
        if redirect_url:
            # The client (likely a webpage) has requested that we signal
            # failure by redirecting to a specific URL.
            redirect_url = utils.append_url_params(
                redirect_url,
                error="true",
                status=status_code,
                message=httplib.responses[status_code],
                reason=reason,
                detail=json.dumps(detail))
            self.redirect(redirect_url)
        else:
            # Handle normally.  this will loop back through write_error below.
            tornado.web.RequestHandler.send_error(self,
                                                  status_code=status_code,
                                                  reason=reason,
                                                  detail=detail,
                                                  headers=headers,
                                                  **kwargs)
Пример #6
0
    def send_success(self, status_code=200, data={}):
        """
        If the client has requested a redirect response, serializes the data
        into a redirect.  Otherwise, equivalent to:

        self.set_status(status_code)
        self.finish(data)
        """
        redirect_url = self.get_argument("onsuccess", None)
        if redirect_url:
            redirect_url = utils.append_url_params(
                redirect_url,
                success="true",
                status=status_code,
                message=httplib.responses[status_code],
                data=json.dumps(data))
            self.redirect(redirect_url)
        else:
            self.set_status(status_code)
            if status_code == httplib.NO_CONTENT:
                self.finish()
            else:
                self.finish(data)