Home / Function/ handle_exception() — flask Function Reference

handle_exception() — flask Function Reference

Architecture documentation for the handle_exception() function in app.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  738c25cf_2aef_f422_7e87_9520ea64ce07["handle_exception()"]
  9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5["Flask"]
  738c25cf_2aef_f422_7e87_9520ea64ce07 -->|defined in| 9f8cc56e_d5c4_e7a8_438a_3124c0dd5de5
  fb0e1e8e_33b2_bf68_f1ae_2eace5216191["wsgi_app()"]
  fb0e1e8e_33b2_bf68_f1ae_2eace5216191 -->|calls| 738c25cf_2aef_f422_7e87_9520ea64ce07
  fc3981a8_3dcc_4c39_6581_f5a96d8d0e49["log_exception()"]
  738c25cf_2aef_f422_7e87_9520ea64ce07 -->|calls| fc3981a8_3dcc_4c39_6581_f5a96d8d0e49
  a89c0022_4807_bf16_9be1_6a66f3c78c9f["ensure_sync()"]
  738c25cf_2aef_f422_7e87_9520ea64ce07 -->|calls| a89c0022_4807_bf16_9be1_6a66f3c78c9f
  8a660ad5_27c1_9b88_cf57_c82cb644de6c["finalize_request()"]
  738c25cf_2aef_f422_7e87_9520ea64ce07 -->|calls| 8a660ad5_27c1_9b88_cf57_c82cb644de6c
  style 738c25cf_2aef_f422_7e87_9520ea64ce07 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/flask/app.py lines 896–947

    def handle_exception(self, ctx: AppContext, e: Exception) -> Response:
        """Handle an exception that did not have an error handler
        associated with it, or that was raised from an error handler.
        This always causes a 500 ``InternalServerError``.

        Always sends the :data:`got_request_exception` signal.

        If :data:`PROPAGATE_EXCEPTIONS` is ``True``, such as in debug
        mode, the error will be re-raised so that the debugger can
        display it. Otherwise, the original exception is logged, and
        an :exc:`~werkzeug.exceptions.InternalServerError` is returned.

        If an error handler is registered for ``InternalServerError`` or
        ``500``, it will be used. For consistency, the handler will
        always receive the ``InternalServerError``. The original
        unhandled exception is available as ``e.original_exception``.

        .. versionchanged:: 1.1.0
            Always passes the ``InternalServerError`` instance to the
            handler, setting ``original_exception`` to the unhandled
            error.

        .. versionchanged:: 1.1.0
            ``after_request`` functions and other finalization is done
            even for the default 500 response when there is no handler.

        .. versionadded:: 0.3
        """
        exc_info = sys.exc_info()
        got_request_exception.send(self, _async_wrapper=self.ensure_sync, exception=e)
        propagate = self.config["PROPAGATE_EXCEPTIONS"]

        if propagate is None:
            propagate = self.testing or self.debug

        if propagate:
            # Re-raise if called with an active exception, otherwise
            # raise the passed in exception.
            if exc_info[1] is e:
                raise

            raise e

        self.log_exception(ctx, exc_info)
        server_error: InternalServerError | ft.ResponseReturnValue
        server_error = InternalServerError(original_exception=e)
        handler = self._find_error_handler(server_error, ctx.request.blueprints)

        if handler is not None:
            server_error = self.ensure_sync(handler)(server_error)

        return self.finalize_request(ctx, server_error, from_error_handler=True)

Subdomains

Defined In

Called By

Frequently Asked Questions

What does handle_exception() do?
handle_exception() is a function in the flask codebase, defined in src/flask/app.py.
Where is handle_exception() defined?
handle_exception() is defined in src/flask/app.py at line 896.
What does handle_exception() call?
handle_exception() calls 3 function(s): ensure_sync, finalize_request, log_exception.
What calls handle_exception()?
handle_exception() is called by 1 function(s): wsgi_app.

Analyze Your Own Codebase

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

Try Supermodel Free