MCPcopy
hub / github.com/pallets/flask / handle_exception

Method handle_exception

src/flask/app.py:811–862  ·  view source on GitHub ↗

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``,

(self, e: Exception)

Source from the content-addressed store, hash-verified

809 return self.ensure_sync(handler)(e) # type: ignore[no-any-return]
810
811 def handle_exception(self, e: Exception) -> Response:
812 """Handle an exception that did not have an error handler
813 associated with it, or that was raised from an error handler.
814 This always causes a 500 ``InternalServerError``.
815
816 Always sends the :data:`got_request_exception` signal.
817
818 If :data:`PROPAGATE_EXCEPTIONS` is ``True``, such as in debug
819 mode, the error will be re-raised so that the debugger can
820 display it. Otherwise, the original exception is logged, and
821 an :exc:`~werkzeug.exceptions.InternalServerError` is returned.
822
823 If an error handler is registered for ``InternalServerError`` or
824 ``500``, it will be used. For consistency, the handler will
825 always receive the ``InternalServerError``. The original
826 unhandled exception is available as ``e.original_exception``.
827
828 .. versionchanged:: 1.1.0
829 Always passes the ``InternalServerError`` instance to the
830 handler, setting ``original_exception`` to the unhandled
831 error.
832
833 .. versionchanged:: 1.1.0
834 ``after_request`` functions and other finalization is done
835 even for the default 500 response when there is no handler.
836
837 .. versionadded:: 0.3
838 """
839 exc_info = sys.exc_info()
840 got_request_exception.send(self, _async_wrapper=self.ensure_sync, exception=e)
841 propagate = self.config["PROPAGATE_EXCEPTIONS"]
842
843 if propagate is None:
844 propagate = self.testing or self.debug
845
846 if propagate:
847 # Re-raise if called with an active exception, otherwise
848 # raise the passed in exception.
849 if exc_info[1] is e:
850 raise
851
852 raise e
853
854 self.log_exception(exc_info)
855 server_error: InternalServerError | ft.ResponseReturnValue
856 server_error = InternalServerError(original_exception=e)
857 handler = self._find_error_handler(server_error, request.blueprints)
858
859 if handler is not None:
860 server_error = self.ensure_sync(handler)(server_error)
861
862 return self.finalize_request(server_error, from_error_handler=True)
863
864 def log_exception(
865 self,

Callers 1

wsgi_appMethod · 0.95

Calls 4

log_exceptionMethod · 0.95
ensure_syncMethod · 0.95
finalize_requestMethod · 0.95
_find_error_handlerMethod · 0.80

Tested by

no test coverage detected