Skip to content
Myr

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

5 files+129 added−101 removed

modified:docs/blueprints.rst-1 removed
240 unmodified lines
241242This will link to ``admin.index`` for instance in case the current request243was dispatched to any other admin blueprint endpoint.244245.. _my-blueprint-error-label:245246Blueprint Error Handlers247------------------------248
29 unmodified lines
added:docs/debugging.rst+97 added
1Debugging Application Errors2============================345In Production6-------------78**Do not run the development server, or enable the built-in debugger, in9a production environment.** The debugger allows executing arbitrary10Python code from the browser. It's protected by a pin, but that should11not be relied on for security.1213Use an error logging tool, such as Sentry, as described in14:ref:`error-logging-tools`, or enable logging and notifications as15described in :doc:`/logging`.1617If you have access to the server, you could add some code to start an18external debugger if ``request.remote_addr`` matches your IP. Some IDE19debuggers also have a remote mode so breakpoints on the server can be20interacted with locally. Only enable a debugger temporarily.212223The Built-In Debugger24---------------------2526The built-in Werkzeug development server provides a debugger which shows27an interactive traceback in the browser when an unhandled error occurs28during a request. This debugger should only be used during development.2930.. image:: _static/debugger.png31 :align: center32 :class: screenshot33 :alt: screenshot of debugger in action3435.. warning::3637 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.4142To enable the debugger, run the development server with the43``FLASK_ENV`` environment variable set to ``development``. This puts44Flask in debug mode, which changes how it handles some errors, and45enables the debugger and reloader.4647.. code-block:: text4849 $ export FLASK_ENV=development50 $ flask run5152``FLASK_ENV`` can only be set as an environment variable. When running53from Python code, passing ``debug=True`` enables debug mode, which is54mostly equivalent. Debug mode can be controled separately from55``FLASK_ENV`` with the ``FLASK_DEBUG`` environment variable as well.5657.. code-block:: python5859 app.run(debug=True)6061:doc:`/server` and :doc:`/cli` have more information about running the62debugger, debug mode, and development mode. More information about the63debugger can be found in the `Werkzeug documentation64<https://werkzeug.palletsprojects.com/debug/>`__.656667External Debuggers68------------------6970External debuggers, such as those provided by IDEs, can offer a more71powerful debugging experience than the built-in debugger. They can also72be used to step through code during a request before an error is raised,73or if no error is raised. Some even have a remote mode so you can debug74code running on another machine.7576When using an external debugger, the app should still be in debug mode,77but it can be useful to disable the built-in debugger and reloader,78which can interfere.7980When running from the command line:8182.. code-block:: text8384 $ export FLASK_ENV=development85 $ flask run --no-debugger --no-reload8687When running from Python:8889.. code-block:: python9091 app.run(debug=True, use_debugger=False, use_reloader=False)9293Disabling these isn't required, an external debugger will continue to94work with the following caveats. If the built-in debugger is not95disabled, it will catch unhandled exceptions before the external96debugger can. If the reloader is not disabled, it could cause an97unexpected reload if code changes during debugging.
modified:docs/errorhandling.rst+6 added-59 removed
71 unmodified lines
72- `Getting started with Sentry <https://docs.sentry.io/quickstart/?platform=python>`__73- `Flask-specific documentation <https://docs.sentry.io/platforms/python/flask/>`__747576.. _error-handlers:7776Error Handlers77--------------7879When an error occurs in Flask, an appropriate `HTTP status code
446 unmodified lines
526527Logging528-------529532See :doc:`/logging` for information on how to log exceptions, such as by533emailing them to admins.530See :doc:`/logging` for information about how to log exceptions, such as531by emailing them to admins.532535Debugging Application Errors536============================533538For production applications, configure your application with logging and539notifications as described in :doc:`/logging`. This section provides540pointers when debugging deployment configuration and digging deeper with a541full-featured Python debugger.534Debugging535---------536543When in Doubt, Run Manually544---------------------------545546Having problems getting your application configured for production? If you547have shell access to your host, verify that you can run your application548manually from the shell in the deployment environment. Be sure to run under549the same user account as the configured deployment to troubleshoot permission550issues. You can use Flask's builtin development server with `debug=True` on551your production host, which is helpful in catching configuration issues, but552**be sure to do this temporarily in a controlled environment.** Do not run in553production with `debug=True`.554555556.. _working-with-debuggers:557558Working with Debuggers559----------------------560561The built-in development server provides a :ref:`debug-mode` that shows562an interactive traceback in the browser when an unhandled error occurs563during a request.564565External debuggers, such as those provided by IDEs, can offer a much566more powerful and visual debugging experience. They can also be used to567step through code during a request before an error is raised, or if no568error is raised.569570When using an external debugger, the app should still be in debug mode,571but it can be useful to disable the built-in debugger and reloader,572which can interfere.573574When running from the command line:575576.. code-block:: text577578 $ export FLASK_ENV=development579 $ flask run --no-debugger --no-reload580581When running from Python:582583.. code-block:: python584585 app.run(debug=True, use_debugger=False, use_reloader=False)586587Disabling these isn't required, an external debugger will continue to588work with the following caveats. If the built-in debugger is not589disabled, it will catch unhandled exceptions before the external590debugger can. If the reloader is not disabled, it could cause an591unexpected reload if code changes during debugging.537See :doc:`/debugging` for information about how to debug errors in538development and production.
modified:docs/index.rst+1 added
42 unmodified lines
43 tutorial/index44 templating45 testing46 errorhandling47 debugging48 logging49 config50 signals51 views
39 unmodified lines
modified:docs/quickstart.rst+25 added-41 removed
109 unmodified lines
110111The most common reason is a typo or because you did not actually create an112``app`` object.113114.. _debug-mode:114115Debug Mode116----------117119(Want to just log errors and stack traces? See :doc:`errorhandling`)118The ``flask run`` command can do more than just start the development119server. By enabling debug mode, the server will automatically reload if120code changes, and will show an interactive debugger in the browser if an121error occurs during a request.122121The :command:`flask` script is nice to start a local development server, but122you would have to restart it manually after each change to your code.123That is not very nice and Flask can do better. If you enable debug124support the server will reload itself on code changes, and it will also125provide you with a helpful debugger if things go wrong.123.. image:: _static/debugger.png124 :align: center125 :class: screenshot126 :alt: The interactive debugger in action.127127To enable all development features (including debug mode) you can export128the ``FLASK_ENV`` environment variable and set it to ``development``129before running the server::128.. warning::129131 $ 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.134134(On Windows you need to use ``set`` instead of ``export``.)135To enable all development features, set the ``FLASK_ENV`` environment136variable to ``development`` before calling ``flask run``.137136This does the following things:138.. code-block:: text1391381. it activates the debugger1392. it activates the automatic reloader1403. it enables the debug mode on the Flask application.140 $ export FLASK_ENV=development141 $ flask run142142You can also control debug mode separately from the environment by143exporting ``FLASK_DEBUG=1``.143See also:144145There 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.151147.. admonition:: Attention152149 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**.153154Screenshot of the debugger in action:155156.. image:: _static/debugger.png157 :align: center158 :class: screenshot159 :alt: screenshot of debugger in action160161More information on using the debugger can be found in the `Werkzeug162documentation`_.163164.. _Werkzeug documentation: https://werkzeug.palletsprojects.com/debug/#using-the-debugger165166Have another debugger in mind? See :ref:`working-with-debuggers`.167168153HTML Escaping154-------------155156When returning HTML (the default response type in Flask), any
747 unmodified lines