Exemplo n.º 1
0
    def error(self, request, code, message):
        """
        Writes the proper out an error response message depending on the
        content type in the request
        """
        response_types = self.get_accept(request)
        logger.error(message)

        if "text/html" in response_types:
            request.setResponseCode(code)
            html_error = template.load("error.html")
            result = html_error.render(
                code=code, code_msg=responses[code], message=message)
            request.write(result.encode())

        elif "application/json" in response_types:
            request.setResponseCode(code)
            request.write(dumps({"error": message}))

        else:
            request.setResponseCode(UNSUPPORTED_MEDIA_TYPE)
            error = dumps(
                {"error":
                     "Can only handle one of %s here" % self.ALLOWED_ACCEPT})
            request.write(error)

        request.finish()
Exemplo n.º 2
0
 def template(self):
     """
     Loads the template provided but the partial path in ``TEMPLATE`` on
     the class.
     """
     if self.TEMPLATE is NotImplemented:
         raise NotImplementedError("You must set `TEMPLATE` first")
     return template.load(self.TEMPLATE)
 def test_html(self):
     resource = FakeErrorResource()
     request = DummyRequest()
     request.requestHeaders.setRawHeaders("Accept", ["text/html"])
     resource.setup(request, INTERNAL_SERVER_ERROR, "Test Error")
     resource.render(request)
     self.assertTrue(request.finished)
     self.assertEqual(
         request.responseCode, INTERNAL_SERVER_ERROR, request.written)
     self.assertEqual(
         request.written, [
             template.load("error.html").render(
                 code=INTERNAL_SERVER_ERROR,
                 code_msg=responses[INTERNAL_SERVER_ERROR],
                 message="Test Error")])
 def test_render(self):
     template = load("index.html")
     data = template.render()
     self.assertIn("DOCTYPE html", data)
     self.assertIn("<title>PyFarm:Agent - Information</title>", data)
 def test_loads_deferred(self):
     self.assertIsInstance(load("index.html"), Template)