Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_external_middleware_can_change_severity(self):
def severity_callback(notification):
notification.severity = 'info'
bugsnag.before_notify(severity_callback)
bugsnag.notify(ScaryException('unexpected failover'))
json_body = self.server.received[0]['json_body']
event = json_body['events'][0]
self.assertEqual(event['severity'], 'info')
self.assertEqual(event['severityReason']['type'],
'userCallbackSetSeverity')
def test_notify_unhandled_severity_callback(self):
def callback(report):
report.severity = "info"
bugsnag.before_notify(callback)
bugsnag.notify(ScaryException("unexpected failover"), severity="error")
json_body = self.server.received[0]['json_body']
event = json_body['events'][0]
self.assertFalse(event['unhandled'])
self.assertEqual(event['severityReason'], {
"type": "userCallbackSetSeverity"
})
def test_notify_before_notify_modifying_metadata(self):
def callback(report):
report.meta_data['foo'] = {'sandwich': 'bar'}
bugsnag.before_notify(callback)
bugsnag.notify(ScaryException('unexpected failover'))
json_body = self.server.received[0]['json_body']
event = json_body['events'][0]
self.assertEqual('bar', event['metaData']['foo']['sandwich'])
def test_notify_before_notify_remove_api_key(self):
def callback(report):
report.api_key = None
bugsnag.before_notify(callback)
bugsnag.notify(ScaryException('unexpected failover'))
self.assertEqual(0, len(self.server.received))
def test_middleware_stack_order_legacy(self):
def first_callback(notification):
notification.meta_data['test']['array'].append(1)
def second_callback(notification):
notification.meta_data['test']['array'].append(2)
# Add a regular callback function
bugsnag.before_notify(second_callback)
# Simulate an internal middleware
bugsnag.legacy.configuration.internal_middleware.before_notify(
first_callback)
bugsnag.notify(ScaryException('unexpected failover'),
test={'array': []})
json_body = self.server.received[0]['json_body']
event = json_body['events'][0]
self.assertEqual(event['metaData']['test']['array'], [1, 2])
def test_external_middleware_cannot_change_severity_reason(self):
def severity_reason_callback(notification):
notification.severity_reason['type'] = 'testReason'
bugsnag.before_notify(severity_reason_callback)
bugsnag.notify(ScaryException('unexpected failover'))
json_body = self.server.received[0]['json_body']
event = json_body['events'][0]
self.assertEqual(event['severityReason']['type'], 'handledException')
if log_server == 'bugsnag':
if not BUGSNAG_ERROR:
# Bugsnag logger
bugsnag_api = context_filter.api_key
if bugsnag_api is not None:
bugsnag.configure(
api_key=bugsnag_api,
app_version=info.CNCHI_VERSION,
project_root='/usr/share/cnchi/cnchi',
release_stage=info.CNCHI_RELEASE_STAGE)
bugsnag_handler = BugsnagHandler(api_key=bugsnag_api)
bugsnag_handler.setLevel(logging.WARNING)
bugsnag_handler.setFormatter(formatter)
bugsnag_handler.addFilter(context_filter.filter)
bugsnag.before_notify(context_filter.bugsnag_before_notify_callback)
logger.addHandler(bugsnag_handler)
logging.info(
"Sending Cnchi log messages to bugsnag server (using python-bugsnag).")
else:
logging.warning(
"Cannot read the bugsnag api key, logging to bugsnag is not possible.")
else:
logging.warning(BUGSNAG_ERROR)
else:
# Socket logger
socket_handler = logging.handlers.SocketHandler(
log_server,
logging.handlers.DEFAULT_TCP_LOGGING_PORT)
socket_formatter = logging.Formatter(formatter)
socket_handler.setFormatter(socket_formatter)
logger.addHandler(socket_handler)
'company', {
'name': 'Stark Industries'
}
)
if notification.context == 'GET /crashcallback':
# The callback will evaluate all exceptions, but in this example only errors from @app.route('/crashcallback') will have the below data added to their error reports.
notification.add_tab('Diagnostics', {
'message': 'Flask demo: Everything is fine',
'status': 200,
'password': 'password1' # this will be filtered by your param_filters.
})
# note that if you return false from the callback, this will cancel the entire error report.
# attach your callback to Bugsnag. Important to attach AFTER 'handle_exceptions(app)' above, so that the function will have full access to the exception data.
bugsnag.before_notify(callback)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/crashdict')
def crashdict():
"""Deliberately triggers an unhandled KeyError to be reported by the bugsnag exception handler, and crash the app.
"""
customheader = request.headers['my-custom-header']
return 'Received your header: ' + customheader
@app.route('/crashcallback')
def crashcallback():
'company', {
'name': 'Stark Industries'
}
)
# checks every error, and adds special metadata only when the error class is 'SpecificError', as in crash_with_callback(), below.
if isinstance(notification.exception, SpecificError):
tab = {
'message': 'That\'s not how this works',
'code': 500,
'password': 'ue$hs_9gFsd!kjl41___' # this will be redacted by your filter.
}
notification.add_tab('Diagnostics', tab)
notification.context = 'Check the \'Diagnostics\' tab attached only to SpecificErrors'
# attach the callback function to your Bugsnag client.
bugsnag.before_notify(callback)
# defining our own error class, as an example.
class SpecificError(Exception):
pass
def crash_dict():
"""Deliberately triggers an unhandled KeyError to be reported by the bugsnag exception handler, and crash the app.
"""
things = { 'object': 'value'}
return things['not_a_key']
def crash_callback():
"""Deliberately raises an unhandled error which will have diagnostic data attached by the before_notify() callback above, and crash the app.