rewrite debugging docs, move to separate page
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent d25ee22 · Browse files · 5 files·+129 added−101 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+129 added−101 removed
- other:+129 added−101 removed
Provenance
- Commit
89d1487- Tree
e39b7ee- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
d25ee22
5 files changed+129 added−101 removed
modified:docs/blueprints.rst-1 removed
240 unmodified lines241 242 This will link to ``admin.index`` for instance in case the current request243 was dispatched to any other admin blueprint endpoint.244 245-.. _my-blueprint-error-label:245 246 Blueprint Error Handlers247 ------------------------248 29 unmodified linesadded:docs/debugging.rst+97 added
1+Debugging Application Errors2+============================3+4+5+In Production6+-------------7+8+**Do not run the development server, or enable the built-in debugger, in9+a production environment.** The debugger allows executing arbitrary10+Python code from the browser. It's protected by a pin, but that should11+not be relied on for security.12+13+Use an error logging tool, such as Sentry, as described in14+:ref:`error-logging-tools`, or enable logging and notifications as15+described in :doc:`/logging`.16+17+If you have access to the server, you could add some code to start an18+external debugger if ``request.remote_addr`` matches your IP. Some IDE19+debuggers also have a remote mode so breakpoints on the server can be20+interacted with locally. Only enable a debugger temporarily.21+22+23+The Built-In Debugger24+---------------------25+26+The built-in Werkzeug development server provides a debugger which shows27+an interactive traceback in the browser when an unhandled error occurs28+during a request. This debugger should only be used during development.29+30+.. image:: _static/debugger.png31+ :align: center32+ :class: screenshot33+ :alt: screenshot of debugger in action34+35+.. warning::36+37+ The debugger allows executing arbitrary Python code from the38+ browser. It is protected by a pin, but still represents a major39+ security risk. Do not run the development server or debugger in a40+ production environment.41+42+To enable the debugger, run the development server with the43+``FLASK_ENV`` environment variable set to ``development``. This puts44+Flask in debug mode, which changes how it handles some errors, and45+enables the debugger and reloader.46+47+.. code-block:: text48+49+ $ export FLASK_ENV=development50+ $ flask run51+52+``FLASK_ENV`` can only be set as an environment variable. When running53+from Python code, passing ``debug=True`` enables debug mode, which is54+mostly equivalent. Debug mode can be controled separately from55+``FLASK_ENV`` with the ``FLASK_DEBUG`` environment variable as well.56+57+.. code-block:: python58+59+ app.run(debug=True)60+61+:doc:`/server` and :doc:`/cli` have more information about running the62+debugger, debug mode, and development mode. More information about the63+debugger can be found in the `Werkzeug documentation64+<https://werkzeug.palletsprojects.com/debug/>`__.65+66+67+External Debuggers68+------------------69+70+External debuggers, such as those provided by IDEs, can offer a more71+powerful debugging experience than the built-in debugger. They can also72+be used to step through code during a request before an error is raised,73+or if no error is raised. Some even have a remote mode so you can debug74+code running on another machine.75+76+When using an external debugger, the app should still be in debug mode,77+but it can be useful to disable the built-in debugger and reloader,78+which can interfere.79+80+When running from the command line:81+82+.. code-block:: text83+84+ $ export FLASK_ENV=development85+ $ flask run --no-debugger --no-reload86+87+When running from Python:88+89+.. code-block:: python90+91+ app.run(debug=True, use_debugger=False, use_reloader=False)92+93+Disabling these isn't required, an external debugger will continue to94+work with the following caveats. If the built-in debugger is not95+disabled, it will catch unhandled exceptions before the external96+debugger can. If the reloader is not disabled, it could cause an97+unexpected reload if code changes during debugging.modified:docs/errorhandling.rst+6 added-59 removed
71 unmodified lines72 - `Getting started with Sentry <https://docs.sentry.io/quickstart/?platform=python>`__73 - `Flask-specific documentation <https://docs.sentry.io/platforms/python/flask/>`__74 75 76-.. _error-handlers:77-76 Error Handlers77 --------------78 79 When an error occurs in Flask, an appropriate `HTTP status code446 unmodified lines526 527 Logging528 -------529 532-See :doc:`/logging` for information on how to log exceptions, such as by533-emailing them to admins.530+See :doc:`/logging` for information about how to log exceptions, such as531+by emailing them to admins.532 535-Debugging Application Errors536-============================533 538-For production applications, configure your application with logging and539-notifications as described in :doc:`/logging`. This section provides540-pointers when debugging deployment configuration and digging deeper with a541-full-featured Python debugger.534+Debugging535+---------536 543-When in Doubt, Run Manually544----------------------------545-546-Having problems getting your application configured for production? If you547-have shell access to your host, verify that you can run your application548-manually from the shell in the deployment environment. Be sure to run under549-the same user account as the configured deployment to troubleshoot permission550-issues. You can use Flask's builtin development server with `debug=True` on551-your production host, which is helpful in catching configuration issues, but552-**be sure to do this temporarily in a controlled environment.** Do not run in553-production with `debug=True`.554-555-556-.. _working-with-debuggers:557-558-Working with Debuggers559-----------------------560-561-The built-in development server provides a :ref:`debug-mode` that shows562-an interactive traceback in the browser when an unhandled error occurs563-during a request.564-565-External debuggers, such as those provided by IDEs, can offer a much566-more powerful and visual debugging experience. They can also be used to567-step through code during a request before an error is raised, or if no568-error is raised.569-570-When using an external debugger, the app should still be in debug mode,571-but it can be useful to disable the built-in debugger and reloader,572-which can interfere.573-574-When running from the command line:575-576-.. code-block:: text577-578- $ export FLASK_ENV=development579- $ flask run --no-debugger --no-reload580-581-When running from Python:582-583-.. code-block:: python584-585- app.run(debug=True, use_debugger=False, use_reloader=False)586-587-Disabling these isn't required, an external debugger will continue to588-work with the following caveats. If the built-in debugger is not589-disabled, it will catch unhandled exceptions before the external590-debugger can. If the reloader is not disabled, it could cause an591-unexpected reload if code changes during debugging.537+See :doc:`/debugging` for information about how to debug errors in538+development and production.modified:docs/index.rst+1 added
42 unmodified lines43 tutorial/index44 templating45 testing46 errorhandling47+ debugging48 logging49 config50 signals51 views39 unmodified linesmodified:docs/quickstart.rst+25 added-41 removed
109 unmodified lines110 111 The most common reason is a typo or because you did not actually create an112 ``app`` object.113 114-.. _debug-mode:114 115 Debug Mode116 ----------117 119-(Want to just log errors and stack traces? See :doc:`errorhandling`)118+The ``flask run`` command can do more than just start the development119+server. By enabling debug mode, the server will automatically reload if120+code changes, and will show an interactive debugger in the browser if an121+error occurs during a request.122 121-The :command:`flask` script is nice to start a local development server, but122-you would have to restart it manually after each change to your code.123-That is not very nice and Flask can do better. If you enable debug124-support the server will reload itself on code changes, and it will also125-provide you with a helpful debugger if things go wrong.123+.. image:: _static/debugger.png124+ :align: center125+ :class: screenshot126+ :alt: The interactive debugger in action.127 127-To enable all development features (including debug mode) you can export128-the ``FLASK_ENV`` environment variable and set it to ``development``129-before running the server::128+.. warning::129 131- $ export FLASK_ENV=development132- $ flask run130+ The debugger allows executing arbitrary Python code from the131+ browser. It is protected by a pin, but still represents a major132+ security risk. Do not run the development server or debugger in a133+ production environment.134 134-(On Windows you need to use ``set`` instead of ``export``.)135+To enable all development features, set the ``FLASK_ENV`` environment136+variable to ``development`` before calling ``flask run``.137 136-This does the following things:138+.. code-block:: text139 138-1. it activates the debugger139-2. it activates the automatic reloader140-3. it enables the debug mode on the Flask application.140+ $ export FLASK_ENV=development141+ $ flask run142 142-You can also control debug mode separately from the environment by143-exporting ``FLASK_DEBUG=1``.143+See also:144 145-There are more parameters that are explained in :doc:`/server`.145+- :doc:`/server` and :doc:`/cli` for information about running in146+ development mode.147+- :doc:`/debugging` for information about using the built-in debugger148+ and other debuggers.149+- :doc:`/logging` and :doc:`/errorhandling` to log errors and display150+ nice error pages.151 147-.. admonition:: Attention152 149- Even though the interactive debugger does not work in forking environments150- (which makes it nearly impossible to use on production servers), it still151- allows the execution of arbitrary code. This makes it a major security risk152- and therefore it **must never be used on production machines**.153-154-Screenshot of the debugger in action:155-156-.. image:: _static/debugger.png157- :align: center158- :class: screenshot159- :alt: screenshot of debugger in action160-161-More information on using the debugger can be found in the `Werkzeug162-documentation`_.163-164-.. _Werkzeug documentation: https://werkzeug.palletsprojects.com/debug/#using-the-debugger165-166-Have another debugger in mind? See :ref:`working-with-debuggers`.167-168-153 HTML Escaping154 -------------155 156 When returning HTML (the default response type in Flask), any747 unmodified lines