def zpublisher_exception_hook_wrapper_wrapper(published, REQUEST, t, v, traceback): """Wrapper around linkintegrity's wrapper around zopes exception hook. This allows us to hook into the exception handling for reporting exceptions. """ report_exception = True args = [published, REQUEST, t, v, traceback] try: try: return zpublisher_exception_hook_wrapper(*args) except Retry: # There was probably a conflict, resulting in a Retry # and the request will be done once again completely. report_exception = False raise except ConflictError: # This request was probably executed 3 times # (with Retry-exceptions, see above) but could not be # finished because of a ConflictError. # We want to report the ConflictError now, that's why # we replace the previous exception (which was probably # a Retry exception) with our ConflictError. args[-3:] = sys.exc_info() report_exception = True raise finally: if report_exception: maybe_report_exception(*args)
def exception_hook_wrapper(self, published, REQUEST, t, v, traceback): report_exception = True args = [published, REQUEST, t, v, traceback] try: try: return self.__ori_call__(*args) except Retry: # There was probably a conflict, resulting in a Retry # and the request will be done once again completely. report_exception = False raise except ConflictError: # This request was probably executed 3 times # (with Retry-exceptions, see above) but could not be # finished because of a ConflictError. # We want to report the ConflictError now, that's why # we replace the previous exception (which was probably # a Retry exception) with our ConflictError. args[-3:] = sys.exc_info() report_exception = True raise finally: if report_exception: maybe_report_exception(*args)
def test_silent_handling_of_request_preparation_exceptions(self): """When an exception happens while preparing the request, the exception should be catched and reported as raven_meta_error. """ os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.assertEquals(0, len(get_raven_client().captureException_calls)) reporter.maybe_report_exception(None, None, KeyError, None, None) self.assertEquals(1, len(get_raven_client().captureException_calls), 'Expected exactly one error to be reported.') call, = get_raven_client().captureException_calls self.assertIn('raven_meta_error', call['data']['extra'])
def __call__(self, *args, **kwargs): try: return super(SafeCall, self).__call__(*args, **kwargs) except (ConflictError, KeyboardInterrupt): raise except tuple(to_re_raise): raise except Exception: self.request.response.setHeader("Content-type", "text/x.traceback") e_type, e_value, tb = sys.exc_info() if HAS_RAVEN: maybe_report_exception(self.context, self.request, e_type, e_value, tb) return ''.join(traceback.format_exception(e_type, e_value, tb))
def sentry_except_hook(exc_type, exc, tb): """Custom excepthook to log unhandled exceptions to Sentry and logfile. We need to install this one ourselves, because ZPublisher isn't in play during a bin/instance [zopectl_cmd] script, so we don't get to piggy-back on zpublisher_exception_hook_wrapper. This isn't perfect: We don't have a context (because no traversal is happening) and we get a URL like 'http://foo' reported to Sentry, but it should be good enough to at least notice things going wrong. """ # Log exception to logfile logger.error(''.join(traceback.format_exception(exc_type, exc, tb))) # Log exception to sentry context = None request = getRequest() maybe_report_exception(context, request, exc_type, exc, tb) return sys.__excepthook__(exc_type, exc, tb)
def dispatch_notifications(self, activity): not_dispatched = [] notifications = activity.notifications for notification in notifications: if not self.dispatch_needed(notification): continue try: self.dispatch_notification(notification) except ConflictError: raise except BaseException: e_type, e_value, tb = sys.exc_info() if HAS_RAVEN: maybe_report_exception(self.context, self.request, e_type, e_value, tb) not_dispatched.append(notifications) formatted_traceback = ''.join(traceback.format_exception(e_type, e_value, tb)) logger.error('Exception while dispatch activity (MailDispatcher):\n%s', formatted_traceback) return not_dispatched