def test_unhandled_error(self):
     handler = ErrorHandler(self.app)
     with self.app.test_request_context("/"):
         response = None
         try:
             self.raise_exception()
         except Exception as exception:
             response = handler.handle(exception)
         assert 500 == response.status_code
         assert "An internal error occured" == response.data.decode("utf8")
    def test_handle_error_as_problem_detail_document(self):
        handler = ErrorHandler(self.app)
        with self.app.test_request_context("/"):
            try:
                self.raise_exception(CanBeProblemDetailDocument)
            except Exception as exception:
                response = handler.handle(exception)

            assert 400 == response.status_code
            data = json.loads(response.data.decode("utf8"))
            assert INVALID_URN.title == data["title"]

            # Since we are not in debug mode, the debug_message is
            # destroyed.
            assert "debug_message" not in data
    def test_unhandled_error_debug(self):
        # Set the sitewide log level to DEBUG to get a stack trace
        # instead of a generic error message.
        handler = ErrorHandler(self.app)
        self.activate_debug_mode()

        with self.app.test_request_context("/"):
            response = None
            try:
                self.raise_exception()
            except Exception as exception:
                response = handler.handle(exception)
            assert 500 == response.status_code
            assert response.data.startswith(
                b"Traceback (most recent call last)")
    def test_handle_error_as_problem_detail_document_debug(self):
        # When in debug mode, the debug_message is preserved and a
        # stack trace is appended to it.
        handler = ErrorHandler(self.app)
        self.activate_debug_mode()
        with self.app.test_request_context("/"):
            try:
                self.raise_exception(CanBeProblemDetailDocument)
            except Exception as exception:
                response = handler.handle(exception)

            assert 400 == response.status_code
            data = json.loads(response.data.decode("utf8"))
            assert INVALID_URN.title == data["title"]
            assert data["debug_message"].startswith(
                "A debug_message which should only appear in debug mode.\n\n"
                "Traceback (most recent call last)")