Skip to content

Commit

Permalink
Add displayTracebacks argument to Klein.run.
Browse files Browse the repository at this point in the history
This enables per-Klein app control of traceback display.

Fixes #112
  • Loading branch information
evilham committed Oct 10, 2018
1 parent 11806a2 commit 54f1710
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Paul Hummer
Russel Haering
Tom Most
Wilfredo Sánchez Vega
Evilham
68 changes: 68 additions & 0 deletions docs/examples/disablingtracebacks.rst
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)
12 changes: 10 additions & 2 deletions src/klein/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def urlFor(self, request, endpoint, values=None, method=None,


def run(self, host=None, port=None, logFile=None,
endpoint_description=None):
endpoint_description=None, displayTracebacks=True):
"""
Run a minimal twisted.web server on the specified C{port}, bound to the
interface specified by C{host} and logging to C{logFile}.
Expand All @@ -399,6 +399,10 @@ def run(self, host=None, port=None, logFile=None,
protocol, port and interface. May contain other optional arguments,
e.g. to use SSL: "ssl:443:privateKey=key.pem:certKey=crt.pem"
@type endpoint_description: str
@param displayTracebacks: Weather a processing error will result in
a page displaying the traceback with debugging information or not.
@type displayTracebacks: bool
"""
if logFile is None:
logFile = sys.stdout
Expand All @@ -410,7 +414,11 @@ def run(self, host=None, port=None, logFile=None,
host)

endpoint = endpoints.serverFromString(reactor, endpoint_description)
endpoint.listen(Site(self.resource()))

site = Site(self.resource())
site.displayTracebacks = displayTracebacks

endpoint.listen(site)
reactor.run()


Expand Down

0 comments on commit 54f1710

Please sign in to comment.