Public » FogBugz Middleware
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.2 and tip

add support for optional 404 reporting (case 86)

Changeset ef4b29454656

Parent ee866cdf13b0

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at ef4b29454656 Showing diff from parent ee866cdf13b0 Diff from another changeset...

 
 
1
2
 
 
3
4
5
 
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
16
 
26
27
28
29
 
30
31
32
 
34
35
36
37
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
 
3
4
5
6
7
 
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
 
52
53
54
 
55
56
57
58
 
60
61
62
 
 
 
 
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
@@ -1,5 +1,7 @@
+import re  import sys -import urllib, urllib2 +import urllib +import urllib2  from django.conf import settings  from django.http import Http404   @@ -11,6 +13,30 @@
  allow processing to continue--meaning that if you have Django   set to send an email on error, it will continue to do so."""   + def process_response(self, request, response): + if response.status_code == 404: + if settings.FOGBUGZ_REPORT_BROKEN_LINKS: + domain = request.get_host() + referer = request.META.get('HTTP_REFERER', None) + path = request.get_full_path() + is_internal = _is_internal_request(domain, referer) + if referer and not _is_ignorable_404(path) and is_internal: + ua = request.META.get('HTTP_USER_AGENT', '<none>') + ip = request.META.get('REMOTE_ADDR', '<none>') + + bug = {'ScoutUserName': settings.FOGBUGZ_USERNAME, + 'ScoutProject': settings.FOGBUGZ_PROJECT, + 'ScoutArea': settings.FOGBUGZ_AREA, + 'Description': 'Broken %slink %s on %s' % ((is_internal and 'INTERNAL ' or ''), request.path, domain), + 'Extra': 'Referrer: %s\nRequested URL: %s\nUser agent:%s\nIP address: %s\n' \ + % (referer, request.get_full_path(), ua, ip)} + + try: + urllib2.urlopen(settings.FOGBUGZ_URL, urllib.urlencode(bug)) + except: + pass + return response +   def process_exception(self, request, exception):   # Do not file 404 errors in FogBugz   if isinstance(exception, Http404): @@ -26,7 +52,7 @@
  request_repr = repr(request)   except:   request_repr = 'Request repr() unavailable' - bug["Extra"] = '%s\n\n%s' % (self._get_traceback(sys.exc_info()), request_repr) + bug["Extra"] = '%s\n\n%s' % (_get_traceback(sys.exc_info()), request_repr)     try:   urllib2.urlopen(settings.FOGBUGZ_URL, urllib.urlencode(bug)) @@ -34,7 +60,24 @@
  # Don't throw an exception within the error handler! Bad!   pass   - def _get_traceback(self, exc_info): - """Helper function to return the traceback as a string""" - import traceback - return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) +def _get_traceback(self, exc_info): + """Helper function to return the traceback as a string""" + import traceback + return '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info()))) + +def _is_internal_request(domain, referer): + """Returns true if the referring URL is the same domain as the current + request.""" + return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer) + +def _is_ignorable_404(uri): + """ + Returns True if a 404 at the given URL *shouldn't* notify the site managers. + """ + for start in settings.IGNORABLE_404_STARTS: + if uri.startswith(start): + return True + for end in settings.IGNORABLE_404_ENDS: + if uri.endswith(end): + return True + return False \ No newline at end of file