Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include django logged user in django Traceback error

Tags:

django

What is the easist way to include username, first and last name and e-amil in django Traceback error.

I know that the way is create a custom error report:

  1. Create a new class that innherit from django.views.debug.SafeExceptionReporterFilter
  2. Set DEFAULT_EXCEPTION_REPORTER_FILTER

But, what method a should overwrite to receive traceback with also this information?

I would like that my treceback look likes:

Traceback (most recent call last):

 File "/usr...o/core/handlers/base.py", line 89, in get_response
   response = middleware_method(request)

 File "/.../g...ap/utils/middleware.py", line 23,...
   if elapsedTime.min > 15:

TypeError: can't compare datetime.timedelta to int

Logged user information:
User:    pepito
name:    Pepito Grillo
e-mail:  [email protected]
like image 236
dani herrera Avatar asked Feb 15 '12 13:02

dani herrera


Video Answer


2 Answers

My trivial solution (works in django 1.5)

settings.py:

MIDDLEWARE_CLASSES = (
    ...
    'utilities.custom_middleware.UserTracebackMiddleware',
    ...
)

custom_middleware.py:

class UserTracebackMiddleware(object):
    """
    Adds user to request context during request processing, so that they
    show up in the error emails.
    """
    def process_exception(self, request, exception):
        if request.user.is_authenticated():
            request.META['AUTH_USER'] = unicode(request.user.username)
        else:
            request.META['AUTH_USER'] = "Anonymous User"

hope it helps

like image 76
Andrea Rabbaglietti Avatar answered Oct 19 '22 19:10

Andrea Rabbaglietti


I did it using Custom Middleware. I'm not sure this is the best answer, but it is how I solved it for my project.

settings.py:

MIDDLEWARE_CLASSES = (
    ...
    'utilities.custom_middleware.CustomMiddleware',
    ...
)

utilities/custom_middleware.py:

from utilities.request import AddRequestDetails

class CustomMiddleware(object):
"""
    Adds user details to request context during request processing, so that they
    show up in the error emails. Add to settings.MIDDLEWARE_CLASSES and keep it
    outermost(i.e. on top if possible). This allows it to catch exceptions in
    other middlewares as well.
"""

    def process_exception(self, request, exception):
        """
        Process the request to add some variables to it.
        """

        # Add other details about the user to the META CGI variables.
        try:
            if request.user.is_authenticated():
                AddRequestDetails(request)
                request.META['AUTH_VIEW_ARGS'] = str(view_args)
                request.META['AUTH_VIEW_CALL'] = str(view_func)
                request.META['AUTH_VIEW_KWARGS'] = str(view_kwargs)
        except:
            pass

utilities/request.py:

def AddRequestDetails(request):
"""
    Adds details about the user to the request, so any traceback will include the 
    details. Good for troubleshooting; this will be included in the email sent to admins 
    on error. 
"""
if request.user.is_anonymous():
    request.META['AUTH_NAME'] = "Anonymous User"
    request.META['AUTH_USER'] = "Anonymous User"
    request.META['AUTH_USER_EMAIL'] = ""
    request.META['AUTH_USER_ID'] = 0
    request.META['AUTH_USER_IS_ACTIVE'] = False
    request.META['AUTH_USER_IS_SUPERUSER'] = False
    request.META['AUTH_USER_IS_STAFF'] = False
    request.META['AUTH_USER_LAST_LOGIN'] = ""
else:
    request.META['AUTH_NAME'] = str(request.user.first_name) + " " + str(request.user.last_name)
    request.META['AUTH_USER'] = str(request.user.username)
    request.META['AUTH_USER_EMAIL'] = str(request.user.email)
    request.META['AUTH_USER_ID'] = str(request.user.id)
    request.META['AUTH_USER_IS_ACTIVE'] = str(request.user.is_active)
    request.META['AUTH_USER_IS_SUPERUSER'] = str(request.user.is_superuser)
    request.META['AUTH_USER_IS_STAFF'] = str(request.user.is_staff)
    request.META['AUTH_USER_LAST_LOGIN'] = str(request.user.last_login)
like image 26
Furbeenator Avatar answered Oct 19 '22 19:10

Furbeenator