sprockets.http/docs/api.rst

109 lines
3.8 KiB
ReStructuredText
Raw Normal View History

2015-07-22 18:04:26 +00:00
API Documentation
=================
Application Runner
------------------
.. autofunction:: sprockets.http.run
2015-07-22 18:04:26 +00:00
2015-09-24 18:55:13 +00:00
Application Callbacks
~~~~~~~~~~~~~~~~~~~~~
2015-09-24 18:55:13 +00:00
Starting with version 0.4.0, :func:`sprockets.http.run` augments the
:class:`tornado.web.Application` instance with a new attribute named
``runner_callbacks`` which is a dictionary of lists of functions to
call when specific events occur. The following events are supported:
:before_run:
This set of callbacks is invoked after Tornado forks sub-processes
(based on the ``number_of_procs`` setting) and before
:meth:`~tornado.ioloop.IOLoop.start` is called. Callbacks can
safely access the :class:`~tornado.ioloop.IOLoop` without causing
the :meth:`~tornado.ioloop.IOLoop.start` method to explode.
If any callback raises an exception, then the application is
terminated **before** the IOLoop is started.
2016-03-11 15:29:14 +00:00
:on_start:
This set of callbacks is invoked after Tornado forks sub-processes
(based on the ``number_of_procs`` setting) and **after**
:meth:`~tornado.ioloop.IOLoop.start` is called.
:shutdown:
When the application receives a stop signal, it will run each of the
callbacks before terminating the application instance. Exceptions
raised by the callbacks are simply logged.
2015-09-24 18:55:13 +00:00
See :func:`sprockets.http.run` for a detailed description of how to
install the runner callbacks.
2015-07-22 18:04:26 +00:00
Internal Interfaces
~~~~~~~~~~~~~~~~~~~
2015-07-22 18:04:26 +00:00
.. automodule:: sprockets.http.runner
:members:
Response Logging
----------------
Version 0.5.0 introduced the :mod:`sprockets.http.mixins` module with
two simple classes - :class:`~sprockets.http.mixins.LoggingHandler`
and :class:`~sprockets.http.mixins.ErrorLogger`. Together they ensure
that errors emitted from your handlers will be logged in a consistent
manner. All too often request handlers simply call ``write_error``
to report a failure to the caller with code that looks something like:
.. code-block:: python
class MyHandler(web.RequestHandler):
def get(self):
try:
do_something()
except Failure:
self.send_error(500, reason='Uh oh')
return
This makes debugging an application fun since your caller generally
has more information about the failure than you do :/
By adding :class:`~sprockets.http.mixins.ErrorLogger` into the inheritance
chain, your error will be emitted to the application log as if you had
written the following instead:
.. code-block:: python
class MyHandler(web.RequestHandler):
def initialize(self):
super(MyHandler, self).initialize()
self.logger = logging.getLogger('MyHandler')
def get(self):
try:
do_something()
except Failure:
self.logger.error('%s %s failed with %d: %s',
self.request.method, self.request.uri,
500, 'Uh oh')
self.send_error(500, reason='Uh oh')
return
It doesn't look like much, but the error reporting is a little more
interesting than that -- 4XX errors are reported as a warning,
exceptions will include the stack traces, etc.
.. autoclass:: sprockets.http.mixins.LoggingHandler
:members:
.. autoclass:: sprockets.http.mixins.ErrorLogger
:members:
2015-11-19 22:29:07 +00:00
Standardized Error Response Documents
-------------------------------------
Version 0.5.0 also introduced the :class:`~sprockets.http.mixins.ErrorWriter`
class which implements ``write_error`` to provide a standard machine-readable
document response instead of the default HTML response that Tornado implements.
If :class:`~sprockets.mixins.mediatype.ContentMixin` is being used as well,
``write_error`` will use
:meth:`~sprockets.mixins.mediatype.ContentMixin.send_response` to send the
document, otherwise it is sent as JSON.
2015-11-19 22:29:07 +00:00
.. autoclass:: sprockets.http.mixins.ErrorWriter
:members: