Return meaningful error when there is no cached result.

Previously it was crashing as it was trying to access an unreferenced
variables (query_result).
This commit is contained in:
Arik Fraimovich 2016-06-14 10:41:01 +03:00
parent c2e3637dce
commit 68465b0c60
2 changed files with 10 additions and 1 deletions

View File

@ -115,6 +115,8 @@ class QueryResultResource(BaseResource):
if query_result_id: if query_result_id:
query_result = get_object_or_404(models.QueryResult.get_by_id_and_org, query_result_id, self.current_org) query_result = get_object_or_404(models.QueryResult.get_by_id_and_org, query_result_id, self.current_org)
else:
query_result = None
if query_result: if query_result:
require_access(query_result.data_source.groups, self.current_user, view_only) require_access(query_result.data_source.groups, self.current_user, view_only)
@ -156,7 +158,7 @@ class QueryResultResource(BaseResource):
return response return response
else: else:
abort(404) abort(404, message='No cached result found for this query.')
def make_json_response(self, query_result): def make_json_response(self, query_result):
data = json.dumps({'query_result': query_result.to_dict()}, cls=utils.JSONEncoder) data = json.dumps({'query_result': query_result.to_dict()}, cls=utils.JSONEncoder)

View File

@ -16,6 +16,12 @@ class TestQueryResultsCacheHeaders(BaseTestCase):
rv = self.make_request('get', '/api/queries/{}/results.json'.format(query.id)) rv = self.make_request('get', '/api/queries/{}/results.json'.format(query.id))
self.assertNotIn('Cache-Control', rv.headers) self.assertNotIn('Cache-Control', rv.headers)
def test_returns_404_if_no_cached_result_found(self):
query = self.factory.create_query(latest_query_data=None)
rv = self.make_request('get', '/api/queries/{}/results.json'.format(query.id))
self.assertEqual(404, rv.status_code)
class TestQueryResultListAPI(BaseTestCase): class TestQueryResultListAPI(BaseTestCase):
def test_get_existing_result(self): def test_get_existing_result(self):
@ -107,3 +113,4 @@ class TestQueryResultAPI(BaseTestCase):
rv = self.make_request('get', '/api/query_results/{}'.format(query_result.id)) rv = self.make_request('get', '/api/query_results/{}'.format(query_result.id))
self.assertEquals(rv.status_code, 200) self.assertEquals(rv.status_code, 200)