Home / Class/ TestGenericHandlers Class — flask Architecture

TestGenericHandlers Class — flask Architecture

Architecture documentation for the TestGenericHandlers class in test_user_error_handler.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  d43f613b_526f_83b2_b03e_180d5f56de23["TestGenericHandlers"]
  f3be1606_1a20_1a1b_2703_48b5b295bc8d["test_user_error_handler.py"]
  d43f613b_526f_83b2_b03e_180d5f56de23 -->|defined in| f3be1606_1a20_1a1b_2703_48b5b295bc8d
  c2ac7b10_8aea_b118_3419_564e590c25c0["app()"]
  d43f613b_526f_83b2_b03e_180d5f56de23 -->|method| c2ac7b10_8aea_b118_3419_564e590c25c0
  83c9c51e_e291_f343_62c6_349a614281ff["report_error()"]
  d43f613b_526f_83b2_b03e_180d5f56de23 -->|method| 83c9c51e_e291_f343_62c6_349a614281ff
  5132b7d1_5ccf_846d_45dc_9aab3e67c6af["test_handle_class_or_code()"]
  d43f613b_526f_83b2_b03e_180d5f56de23 -->|method| 5132b7d1_5ccf_846d_45dc_9aab3e67c6af
  4146f47c_bb85_f8c0_436a_b7db8b76fad3["test_handle_generic_http()"]
  d43f613b_526f_83b2_b03e_180d5f56de23 -->|method| 4146f47c_bb85_f8c0_436a_b7db8b76fad3
  41ae651d_a7f6_1444_6a45_736aa21b18e7["test_handle_generic()"]
  d43f613b_526f_83b2_b03e_180d5f56de23 -->|method| 41ae651d_a7f6_1444_6a45_736aa21b18e7

Relationship Graph

Source Code

tests/test_user_error_handler.py lines 217–295

class TestGenericHandlers:
    """Test how very generic handlers are dispatched to."""

    class Custom(Exception):
        pass

    @pytest.fixture()
    def app(self, app):
        @app.route("/custom")
        def do_custom():
            raise self.Custom()

        @app.route("/error")
        def do_error():
            raise KeyError()

        @app.route("/abort")
        def do_abort():
            flask.abort(500)

        @app.route("/raise")
        def do_raise():
            raise InternalServerError()

        app.config["PROPAGATE_EXCEPTIONS"] = False
        return app

    def report_error(self, e):
        original = getattr(e, "original_exception", None)

        if original is not None:
            return f"wrapped {type(original).__name__}"

        return f"direct {type(e).__name__}"

    @pytest.mark.parametrize("to_handle", (InternalServerError, 500))
    def test_handle_class_or_code(self, app, client, to_handle):
        """``InternalServerError`` and ``500`` are aliases, they should
        have the same behavior. Both should only receive
        ``InternalServerError``, which might wrap another error.
        """

        @app.errorhandler(to_handle)
        def handle_500(e):
            assert isinstance(e, InternalServerError)
            return self.report_error(e)

        assert client.get("/custom").data == b"wrapped Custom"
        assert client.get("/error").data == b"wrapped KeyError"
        assert client.get("/abort").data == b"direct InternalServerError"
        assert client.get("/raise").data == b"direct InternalServerError"

    def test_handle_generic_http(self, app, client):
        """``HTTPException`` should only receive ``HTTPException``
        subclasses. It will receive ``404`` routing exceptions.
        """

        @app.errorhandler(HTTPException)
        def handle_http(e):
            assert isinstance(e, HTTPException)
            return str(e.code)

        assert client.get("/error").data == b"500"
        assert client.get("/abort").data == b"500"
        assert client.get("/not-found").data == b"404"

    def test_handle_generic(self, app, client):
        """Generic ``Exception`` will handle all exceptions directly,
        including ``HTTPExceptions``.
        """

        @app.errorhandler(Exception)
        def handle_exception(e):
            return self.report_error(e)

        assert client.get("/custom").data == b"direct Custom"
        assert client.get("/error").data == b"direct KeyError"
        assert client.get("/abort").data == b"direct InternalServerError"
        assert client.get("/not-found").data == b"direct NotFound"

Frequently Asked Questions

What is the TestGenericHandlers class?
TestGenericHandlers is a class in the flask codebase, defined in tests/test_user_error_handler.py.
Where is TestGenericHandlers defined?
TestGenericHandlers is defined in tests/test_user_error_handler.py at line 217.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free