def run(self): FailUrl = get_model('smoke', 'failurl') c = self.get_client() for smoke_url in self.get_urls(): FailUrl.objects.filter(url=smoke_url.url).delete() failurl = FailUrl(url=smoke_url.url) try: response = c.get(smoke_url.url) if response.status_code != 200: logger.error('status code %s from %s' % (response.status_code, smoke_url.url)) failurl.reason = 'status code %s' % response.status_code failurl.save() failurl.tags.add(smoke_url.tags) continue except Exception as e: if sentry_exception_handler: sentry_exception_handler(request=response.request) exc_type, exc_value, exc_tb = sys.exc_info() print ''.join(traceback.format_exception( exc_type, exc_value, exc_tb)) logger.error('exception %s in %s' % (e, smoke_url.url)) failurl.traceback = pickle.dumps(traceback.format_exception( exc_type, exc_value, exc_tb)) failurl.exception = unicode(e) failurl.reason = 'exception' failurl.save() failurl.tags.add(smoke_url.tags) continue # should be ok FailUrl.objects.filter(url=smoke_url.url).delete()
def testUseLogging(self): Message.objects.all().delete() GroupedMessage.objects.all().delete() request = RF.get("/", REMOTE_ADDR="127.0.0.1:8000") try: Message.objects.get(id=999999999) except Message.DoesNotExist, exc: sentry_exception_handler(request=request, sender=self)
def testMiddleware(self): Message.objects.all().delete() GroupedMessage.objects.all().delete() request = RF.get("/", REMOTE_ADDR="127.0.0.1:8000") try: Message.objects.get(id=999999999) except Message.DoesNotExist, exc: sentry_exception_handler(request=request, sender=self)
self.assertEquals(last.logger, 'root') self.assertEquals(last.class_name, 'DoesNotExist') self.assertEquals(last.level, logging.ERROR) self.assertEquals(last.message, smart_unicode(exc)) settings.USE_LOGGING = True logger = logging.getLogger('sentry') for h in logger.handlers: logger.removeHandler(h) logger.addHandler(logging.StreamHandler()) try: Message.objects.get(id=999999999) except Message.DoesNotExist, exc: sentry_exception_handler(request=request, sender=self) else: self.fail('Expected an exception.') cur = (Message.objects.count(), GroupedMessage.objects.count()) self.assertEquals(cur, (1, 1), 'Assumed logs failed to save. %s' % (cur,)) settings.USE_LOGGING = False def testThrashing(self): settings.THRASHING_LIMIT = 10 settings.THRASHING_TIMEOUT = 60 Message.objects.all().delete() GroupedMessage.objects.all().delete()
def process_exception(self, request, exception): # first, make sure the exception is handled by sentry, if installed # we don't pass through our exceptions unless http_code >= 500, or # the Django 1.3 Http404 exception, since these are raised in normal # circumstances and so aren't useful to Sentry. # # (Sentry should pick up the 404s using the Sentry 404 middleware, # which it separates out from exceptions. Ideally, Sentry would # track other 4xx responses similarly.) if 'sentry' in settings.INSTALLED_APPS: if isinstance(exception, RareHttpResponse) and exception.http_code < 500: pass elif isinstance(exception, Http404) or isinstance(exception, Resolver404): pass else: try: from sentry.client.models import sentry_exception_handler sentry_exception_handler(request=request) except ImportError: pass if settings.DEBUG: # unless 3xx, don't do any smart processing; 404 & 500 will get normal # Django processing, everything else becomes a stacktrace if not isinstance(exception, RareHttpResponse) or exception.http_code // 100 != 3: return None if hasattr(settings, 'EXCEPTIONAL_INVASION') and settings.EXCEPTIONAL_INVASION==True: if isinstance(exception, Http404) or isinstance(exception, Resolver404): exception = HttpNotFound(exception) if isinstance(exception, PermissionDenied): exception = HttpForbidden(exception) if not isinstance(exception, RareHttpResponse): if isinstance(exception, SystemExit): raise else: # we're emulating Django's 500 processing, but using our render system to make it pretty # most importantly, we fire off the ``got_request_exception`` signal so people know something # has gone wrong. We *DON'T* email admins, since we don't really believe that's a good idea -- # you should do something in a signal handler instead (shove it in a queue, db, or something else # more manageable than email; or send an email from the handler). # # (If you disagree, you probably want to refactor the handler in Django itself and expose the # mail admin functionality so we can use it in a couple of lines, rather than copying all the code # out.) receivers = signals.got_request_exception.send(sender=self.__class__, request=request) # FIXME: sender is wrong. It should be the handler class that handled the request # (django.core.handlers.wsgi.WsgiHandler or whatever). We could look through the stack trace # for a subclass of django.core.handlers.base.BaseHandler? exception = HttpServerError(exception) if isinstance(exception, RareHttpResponse): def do_render(template=None): if template: template = 'http_responses/%s.html' % (template,) else: template = 'http_responses/%i.html' % (exception.http_code, ), return self.render( request, template, { 'http_code': exception.http_code, 'http_message': STATUS_CODE_TEXT.get(exception.http_code, 'UNKNOWN'), # TRANSLATE IN TEMPLATE 'http_response_exception': exception, } ) try: response = do_render() except TemplateDoesNotExist: response = do_render('default') response.status_code = exception.http_code exception.augment_response(response) if exception.headers: for k,v in exception.headers.items(): response[k] = v return response return None
def process_exception(self, request, exception): if not isinstance(exception, Http404): return sentry_exception_handler(sender=Sentry404CatchMiddleware, request=request)