-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add displayTracebacks argument to Klein.run.
This enables per-Klein app control of traceback display. Fixes #112
- Loading branch information
Showing
3 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ Paul Hummer | |
Russel Haering | ||
Tom Most | ||
Wilfredo Sánchez Vega | ||
Evilham |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
==================================== | ||
Example -- Disabling traceback pages | ||
==================================== | ||
|
||
If processing a request fails, by default Klein (and Twisted) show an error | ||
page that prints the traceback and other useful debugging information. | ||
|
||
This is not desirable when you deploy code publicly, in such a case, you | ||
should disable this behaviour. | ||
|
||
There are multiple ways to do this, you can do this on a Klein-app scope or | ||
change the default for the python process. | ||
|
||
We will use following Klein app: | ||
|
||
.. code-block:: python | ||
from klein import Klein | ||
app = Klein() | ||
@app.route('/OK') | ||
def requestOK(self, request): | ||
return 'OK' | ||
@app.route('/KO') | ||
def requestKO(self, request): | ||
raise RuntimeError('Oops') | ||
Example - Disable traceback pages on a single Klein app | ||
======================================================= | ||
|
||
This works by passing the ``displayTracebacks`` argument as ``False`` | ||
to ``app.run``. | ||
|
||
.. code-block:: python | ||
if __name__ == '__main__': | ||
import sys | ||
displayTracebacks = '--production' not in sys.args | ||
# The Klein app will not display tracebacks if the script is called | ||
# with a --production argument, but it will otherwise. | ||
app.run('localhost', 8080, displayTracebacks=displayTracebacks) | ||
Example - Disable traceback pages process wide | ||
============================================== | ||
|
||
This method also affects other Twisted Sites. | ||
|
||
Under the hood, Klein uses ``twisted.web.server.Site``, which has an | ||
instance variable ``displayTracebacks`` that defaults to ``True``. | ||
|
||
For the rationale behind that, check | ||
https://twistedmatrix.com/trac/ticket/135 | ||
|
||
.. code-block:: python | ||
# Disable tracebacks by default for any Site object. | ||
from twisted.web.server import Site | ||
Site.displayTracebacks = False | ||
if __name__ == '__main__': | ||
# The Klein app won't display tracebacks | ||
app.run('localhost', 8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters