forked from getsentry/raven-python
/
__init__.py
111 lines (87 loc) · 3.76 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
raven.contrib.django
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import
import logging
import sys
from raven.base import Client
class DjangoClient(Client):
logger = logging.getLogger('sentry.errors.client')
def process(self, **kwargs):
from django.http import HttpRequest
request = kwargs.pop('request', None)
is_http_request = isinstance(request, HttpRequest)
if is_http_request:
if not request.POST and request.raw_post_data:
post_data = request.raw_post_data
else:
post_data = request.POST
if 'data' not in kwargs:
kwargs['data'] = {}
kwargs['data'].update(dict(
META=request.META,
POST=post_data,
GET=request.GET,
COOKIES=request.COOKIES,
))
if hasattr(request, 'user'):
if request.user.is_authenticated():
user_info = {
'is_authenticated': True,
'id': request.user.pk,
'username': request.user.username,
'email': request.user.email,
}
else:
user_info = {
'is_authenticated': False,
}
kwargs['data']['__sentry__']['user'] = user_info
if not kwargs.get('url'):
kwargs['url'] = request.build_absolute_uri()
message_id, checksum = super(DjangoClient, self).process(**kwargs)
if is_http_request:
# attach the sentry object to the request
request.sentry = {
'id': '%s$%s' % (message_id, checksum),
'thrashed': False,
}
return message_id, checksum
def create_from_exception(self, exc_info=None, **kwargs):
"""
Creates an error log from an exception.
"""
from django.template import TemplateSyntaxError
from django.template.loader import LoaderOrigin
new_exc = bool(exc_info)
if not exc_info or exc_info is True:
exc_info = sys.exc_info()
data = kwargs.pop('data', {}) or {}
if '__sentry__' not in data:
data['__sentry__'] = {}
try:
exc_type, exc_value, exc_traceback = exc_info
# As of r16833 (Django) all exceptions may contain a ``django_template_source`` attribute (rather than the
# legacy ``TemplateSyntaxError.source`` check) which describes template information.
if hasattr(exc_value, 'django_template_source') or ((isinstance(exc_value, TemplateSyntaxError) and \
isinstance(getattr(exc_value, 'source', None), (tuple, list)) and isinstance(exc_value.source[0], LoaderOrigin))):
origin, (start, end) = getattr(exc_value, 'django_template_source', exc_value.source)
data['__sentry__']['template'] = (origin.reload(), start, end, origin.name)
kwargs['view'] = origin.loadname
return super(DjangoClient, self).create_from_exception(exc_info, **kwargs)
finally:
if new_exc:
try:
del exc_info
except Exception, e:
self.logger.exception(e)
def create_from_record(self, record, **kwargs):
"""
Creates an error log for a ``logging`` module ``record`` instance.
"""
if not kwargs.get('request'):
kwargs['request'] = record.__dict__.get('request')
return super(DjangoClient, self).create_from_record(record, **kwargs)