How can I configure an exception view in Pyramid for friendly error pages, but still pass errors through to a logger?

StackOverflow https://stackoverflow.com/questions/21803487

  •  12-10-2022
  •  | 
  •  

Question

I'm attempting to set up Sentry for my current web app. It can be configured to act as a logger.

However, I have an Exception view that looks like this:

@view_config(context=Exception,
             renderer='hitchedup:templates/errors/500.html')
def error_view(exc, request):
    """Display a friendly error message"""
    return {}

The idea is that whenever any exception occurs on the site, I can deliver a friendly message to the user.

But, how do I still pass the errors through to the logger? Right now, the Exception view catches all exceptions, but then they never reach the logger.

I tried raising and catching exc inline in the Exception view, then sending it to Sentry through the client directly, but then I don't get the full stacktrace I would get if I caught the exception where it was originally raised.

How can I get both friendly 500 pages and still have good logging and error reporting?

Was it helpful?

Solution

The bare raise keyword is the ticket.

Instead of using logging, capture the Exception in a view with a friendly message. Then, use raise without an argument, which raises the original exception. Catch the exception and use the Sentry client to capture it and pass it along.

The final view looks like this:

from raven.base import Client
from pyramid.view import view_config


@view_config(context=Exception,
             renderer='hitchedup:templates/errors/500.html')
def error(context, request):
    """Display an error message and record it in Sentry."""
    client = Client()
    try:
        raise
    except Exception:
        client.captureException()
    return {}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top