def test_bugsnag_middleware_working(deliver): def BasicWorkingApp(environ, start_response): start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')]) return iter([b'OK']) app = TestApp(BugsnagMiddleware(BasicWorkingApp)) resp = app.get('/', status=200) eq_(resp.body, b'OK') eq_(deliver.call_count, 0)
def test_bugsnag_middleware_working(self): def BasicWorkingApp(environ, start_response): start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')]) return iter([b'OK']) app = TestApp(BugsnagMiddleware(BasicWorkingApp)) resp = app.get('/', status=200) self.assertEqual(resp.body, b'OK') self.assertEqual(0, len(self.server.received))
def test_bugsnag_middleware_crash_on_start(deliver): class CrashOnStartApp(object): def __init__(self, environ, start_response): raise SentinalError("oops") app = TestApp(BugsnagMiddleware(CrashOnStartApp)) assert_raises(SentinalError, lambda: app.get('/beans')) eq_(deliver.call_count, 1) payload = deliver.call_args[0][0] eq_(payload['events'][0]['context'], 'GET /beans') eq_(payload['events'][0]['metaData']['environment']['PATH_INFO'], '/beans')
def test_disable_environment(self): bugsnag.configure(send_environment=False) class CrashOnStartApp(object): def __init__(self, environ, start_response): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashOnStartApp)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] assert 'environment' not in payload['events'][0]['metaData']
def test_bugsnag_middleware_respects_meta_data(deliver): class CrashAfterSettingMetaData(object): def __init__(self, environ, start_response): bugsnag.configure_request(meta_data={"account": {"paying": True}}) def __iter__(self): raise SentinalError("oops") app = TestApp(BugsnagMiddleware(CrashAfterSettingMetaData)) assert_raises(SentinalError, lambda: app.get('/beans')) eq_(deliver.call_count, 1) payload = deliver.call_args[0][0] eq_(payload['events'][0]['metaData']['account'], {"paying": True})
def test_bugsnag_middleware_crash_on_start(self): class CrashOnStartApp(object): def __init__(self, environ, start_response): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashOnStartApp)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertEqual(event['context'], 'GET /beans') self.assertEqual(event['metaData']['environment']['PATH_INFO'], '/beans')
def test_disable_environment(self): bugsnag.configure(send_environment=False) @route('/beans') def index(): raise Exception('oh no!') app = bottle.app() app.catchall = False app = TestApp(BugsnagMiddleware(app)) self.assertRaises(Exception, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] metadata = payload['events'][0]['metaData'] assert 'environment' not in metadata
def test_bugsnag_middleware_respects_user_id(deliver): class CrashAfterSettingUserId(object): def __init__(self, environ, start_response): bugsnag.configure_request(user={ "id": "5", "email": "*****@*****.**", "name": "conrad" }) raise SentinalError("oops") app = TestApp(BugsnagMiddleware(CrashAfterSettingUserId)) assert_raises(SentinalError, lambda: app.get('/beans')) eq_(deliver.call_count, 1) payload = deliver.call_args[0][0] eq_(payload['events'][0]['user']['id'], '5')
def test_bugsnag_middleware_respects_user_id(self): class CrashAfterSettingUserId(object): def __init__(self, environ, start_response): bugsnag.configure_request(user={ "id": "5", "email": "*****@*****.**", "name": "conrad", }) raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashAfterSettingUserId)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] self.assertEqual(payload['events'][0]['user']['id'], '5')
def test_bugsnag_middleware_respects_metadata(self): class CrashAfterSettingMetaData(object): def __init__(self, environ, start_response): bugsnag.configure_request(metadata={"account": {"paying": True}}) def __iter__(self): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashAfterSettingMetaData)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertEqual(event['metaData']['account'], {"paying": True})
def test_bugsnag_middleware_crash_on_iter(self): class CrashOnIterApp: def __init__(self, environ, start_response): pass def __iter__(self): return self def __next__(self): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashOnIterApp)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] event = payload['events'][0] assert 'environment' not in event['metaData']
def test_bugsnag_middleware_crash_on_iter(deliver): class CrashOnIterApp(Iterator): def __init__(self, environ, start_response): pass def __iter__(self): return self def __next__(self): raise SentinalError("oops") app = TestApp(BugsnagMiddleware(CrashOnIterApp)) assert_raises(SentinalError, lambda: app.get('/beans')) eq_(deliver.call_count, 1) payload = deliver.call_args[0][0] eq_(payload['events'][0]['metaData']['environment']['PATH_INFO'], '/beans')
def test_bugsnag_middleware_closes_iterables(deliver): class CrashOnCloseIterable(object): def __init__(self, environ, start_response): start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')]) def __iter__(self): yield 'OK' def close(self): raise SentinalError("oops") app = TestApp(BugsnagMiddleware(CrashOnCloseIterable)) assert_raises(SentinalError, lambda: app.get('/beans')) eq_(deliver.call_count, 1) payload = deliver.call_args[0][0] eq_(payload['events'][0]['metaData']['environment']['PATH_INFO'], '/beans')
def test_read_request_in_callback(self): class MyApp(object): def __init__(self, environ, start_response): raise SentinelError("oops") def callback(event): event.set_user(id=event.request.GET['user_id']) bugsnag.before_notify(callback) app = TestApp(BugsnagMiddleware(MyApp)) with pytest.raises(SentinelError): app.get('/beans?user_id=my_id') assert len(self.server.received) == 1 payload = self.server.received[0]['json_body'] assert payload['events'][0]['user']['id'] == 'my_id'
def test_routing_error(self): @route('/beans') def index(): raise Exception('oh no!') app = bottle.app() app.catchall = False app = TestApp(BugsnagMiddleware(app)) self.assertRaises(Exception, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertTrue(event['unhandled']) self.assertEqual(event['context'], 'GET /beans') self.assertEqual(event['exceptions'][0]['errorClass'], 'Exception') self.assertEqual(event['exceptions'][0]['message'], 'oh no!') runtime_versions = event['device']['runtimeVersions'] self.assertEqual(runtime_versions['bottle'], '0.12.18') assert 'environment' not in event['metaData']
def test_bugsnag_middleware_closes_iterables(self): class CrashOnCloseIterable(object): def __init__(self, environ, start_response): start_response("200 OK", [('Content-Type', 'text/plain; charset=utf-8')]) def __iter__(self): yield 'OK' def close(self): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashOnCloseIterable)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] environ = payload['events'][0]['metaData']['environment'] self.assertEqual(environ['PATH_INFO'], '/beans')
def test_bugsnag_middleware_attaches_unhandled_data(self): class CrashOnStartApp(object): def __init__(self, environ, start_response): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashOnStartApp)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertTrue(event['unhandled']) self.assertEqual( event['severityReason'], { 'type': 'unhandledExceptionMiddleware', 'attributes': { 'framework': 'WSGI' } })
def test_template_error(self): @route('/berries/<variety>') def index(variety): return template('{{type1}} {{type2}}', type1=variety) app = bottle.app() app.catchall = False app = TestApp(BugsnagMiddleware(app)) self.assertRaises(Exception, lambda: app.get('/berries/red')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] event = payload['events'][0] self.assertTrue(event['unhandled']) self.assertEqual(event['context'], 'GET /berries/red') self.assertEqual(event['exceptions'][0]['errorClass'], 'NameError') self.assertEqual(event['exceptions'][0]['message'], "name 'type2' is not defined") assert 'environment' not in event['metaData'] runtime_versions = event['device']['runtimeVersions'] self.assertEqual(runtime_versions['bottle'], bottle.__version__)
def test_bugsnag_middleware_crash_on_close(self): class CrashOnCloseApp(Iterator): def __init__(self, environ, start_response): pass def __iter__(self): return self def __next__(self): raise StopIteration() def close(self): raise SentinelError("oops") app = TestApp(BugsnagMiddleware(CrashOnCloseApp)) self.assertRaises(SentinelError, lambda: app.get('/beans')) self.assertEqual(1, len(self.server.received)) payload = self.server.received[0]['json_body'] environ = payload['events'][0]['metaData']['environment'] self.assertEqual(environ['PATH_INFO'], '/beans')
def __init__(self, telegram_client, debug=False): self.MENU_BUTTON = 'Back to the menu 🏠' self.debug = debug self.default_handler = 'main-menu' self.telegram = telegram_client # Flask web app self.tornado = tornado.web.Application( cookie_secret=os.environ['BOT_SECRET']) self.app = BugsnagMiddleware(tornado.wsgi.WSGIAdapter(self.tornado)) # Dict str -> function with all handlers for messages # and other updates self.handlers = {} self.callback_handlers = {} self.callback_subscriptions = {} self.redis = from_url( os.environ.get('REDIS_URL', 'redis://localhost:6379/0')) self.bytes_fields = [] self.logger = logger # self.logger.addHandler(BugsnagHandler()) slack_handler = SlackHandler(os.environ['SIREN_SLACK_TOKEN']) slack_handler.setLevel(logging.ERROR) self.logger.addHandler(slack_handler) self.logger.setLevel(logging.INFO) self.subscriptions = [] bugsnag.configure(api_key=os.environ['BUGSNAG_API_KEY'], project_root=os.getcwd(), notify_release_stages=[ os.environ.get('BUGSNAG_RELEASE_STAGE', 'production') ])
'status': 400, 'message': 'Bad Payload', 'validationError': str(err) }) ) dataguid = Dataguid(did = uuid,schema_json=payload) dataguid.to_dataguid() return dataguid.update_indexd(user, rev) @app.route('/dataguid:/<path:uuid>', methods = ['GET']) def GetDataguid(uuid): content_type = request.accept_mimetypes.best_match(['text/html', 'application/json', 'application/ld+json']) dataguid = Dataguid(did=uuid) return dataguid.fetch_indexd(content_type, request.args.get('format', 'schema.org')) ############################## # Multipule Identifiers # ############################## from bugsnag.wsgi.middleware import BugsnagMiddleware if __name__=="__main__": full_app = BugsnagMiddleware(app) full_app.run(use_debugger= True, debug=app.debug, use_reloader=True, host='0.0.0.0')
from bugsnag.wsgi.middleware import BugsnagMiddleware from wsgiref.simple_server import make_server from cgi import parse_qs, escape # Create our wsgi app def application(environ, start_response): if environ.get('PATH_INFO', '') == "/": d = parse_qs(environ['QUERY_STRING']) start_response('200 OK', [('Content-Type', 'text/html')]) raise Exception("Something broke") return ["Some output here"] else: start_response('404 NOT FOUND', [('Content-Type', 'text/plain')]) return ['Not Found'] # Configure bugsnag bugsnag.configure(api_key="066f5ad3590596f9aa8d601ea89af845") # Add bugsnag wsgi middleware to app application = BugsnagMiddleware(application) # Start a server print("listening on :8051") httpd = make_server('localhost', 8051, application) httpd.serve_forever()
@route('/') def index(): return static_file('index.html', root='.') @route('/crash') def crash(): """Deliberately raises an unhandled error and crash the app. """ raise Exception('SomethingBad') @route('/handled') def handle_zero_div(): """Deliberately triggers a handled exception, and reports it to Bugsnag. """ try: x = 1 / 0 print("x: ", x) except Exception as e: bugsnag.notify(e) return ('The app hasn\'t crashed, but check https://app.bugsnag.com ' + 'to view notifications') app = app() app.catchall = False wrapped_app = BugsnagMiddleware(app) run(app=wrapped_app, host='localhost', port=8080, debug=True)