{ "html": "
\n

Installation

\n

If you haven’t already, start by downloading Raven. The easiest way is\nwith pip:

\n
pip install raven --upgrade\n
\n
\n
\n\n\n
\n

Setup

\n

The first thing you’ll need to do is to initialize sentry client under\nyour application

\n
import tornado.web\nfrom raven.contrib.tornado import AsyncSentryClient\n\nclass MainHandler(tornado.web.RequestHandler):\n    def get(self):\n        self.write("Hello, world")\n\napplication = tornado.web.Application([\n    (r"/", MainHandler),\n])\napplication.sentry_client = AsyncSentryClient(\n    '___DSN___'\n)\n
\n
\n
\n\n\n
\n

Usage

\n

Once the sentry client is attached to the application, request handlers\ncan automatically capture uncaught exceptions by inheriting the SentryMixin class.

\n
import tornado.web\nfrom raven.contrib.tornado import SentryMixin\n\nclass UncaughtExceptionExampleHandler(\n        SentryMixin, tornado.web.RequestHandler):\n    def get(self):\n        1/0\n
\n
\n

You can also send events manually using the shortcuts defined in SentryMixin.\nThe shortcuts can be used for both asynchronous and synchronous usage.

\n
\n

Asynchronous

\n
import tornado.web\nimport tornado.gen\nfrom raven.contrib.tornado import SentryMixin\n\nclass AsyncMessageHandler(SentryMixin, tornado.web.RequestHandler):\n    @tornado.web.asynchronous\n    @tornado.gen.engine\n    def get(self):\n        self.write("You requested the main page")\n        yield tornado.gen.Task(\n            self.captureMessage, "Request for main page served"\n        )\n        self.finish()\n\nclass AsyncExceptionHandler(SentryMixin, tornado.web.RequestHandler):\n    @tornado.web.asynchronous\n    @tornado.gen.engine\n    def get(self):\n        try:\n            raise ValueError()\n        except Exception as e:\n            response = yield tornado.gen.Task(\n                self.captureException, exc_info=True\n            )\n        self.finish()\n
\n
\n
\n

Tip

\n

The value returned by the yield is a HTTPResponse object.

\n

To dynamically use Python if configuration DSN available, change your base handler on fly and make sure all concrete handlers are imported after this.

\n
\n
from raven.contrib.tornado import SentryMixin\napp.sentry_client = AsyncSentryClient(dsn)\nBaseHandler.__bases__ = (SentryMixin, ) + BaseHandler.__bases__\n
\n
\n
\n
\n
\n
\n

Synchronous

\n
import tornado.web\nfrom raven.contrib.tornado import SentryMixin\n\nclass AsyncExampleHandler(SentryMixin, tornado.web.RequestHandler):\n    def get(self):\n        self.write("You requested the main page")\n        self.captureMessage("Request for main page served")\n
\n
\n
\n
\n", "link": "https://docs.getsentry.com/clients/python/integrations/tornado/", "id": "python-tornado", "name": "Tornado" }