From fee975a413e7ab0248ad45e544f8ddd6db2fc046 Mon Sep 17 00:00:00 2001 From: Matt Jia Date: Aug 14 2017 00:55:05 +0000 Subject: raise a proper error when health checks failed Using assert statements is not encourged in production, because they will get stripped out of .pyo files and not run. Instead, we should raise RuntimeError. --- diff --git a/tests/test_api_v10.py b/tests/test_api_v10.py index 4ff3a05..7412483 100644 --- a/tests/test_api_v10.py +++ b/tests/test_api_v10.py @@ -181,3 +181,8 @@ def test_jsonp(client, session): r = client.get('/api/v1.0/waivers/%s?callback=jsonpcallback' % waiver.id) assert r.mimetype == 'application/javascript' assert 'jsonpcallback' in r.get_data(as_text=True) + +def test_healthcheck(client): + r = client.get('healthcheck') + assert r.status_code == 200 + assert r.get_data(as_text=True) == 'Health check OK' diff --git a/waiverdb/app.py b/waiverdb/app.py index dd09bfb..aa3e486 100644 --- a/waiverdb/app.py +++ b/waiverdb/app.py @@ -88,7 +88,8 @@ def healthcheck(): Returns a 200 response if the application is alive and able to serve requests. """ result = db.session.execute('SELECT 1').scalar() - assert result == 1 + if result != 1: + raise RuntimeError('Unable to communicate with database.') return ('Health check OK', 200, [('Content-Type', 'text/plain')])