From dbc2fdfeff304d56f63b826a10f883f09e5c7490 Mon Sep 17 00:00:00 2001 From: Matthew Templeton Date: Thu, 11 May 2023 12:41:32 -0400 Subject: [PATCH] fix: catch missing esource fields in Holdings search modified: journalsservice/views.py --- journalsservice/views.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/journalsservice/views.py b/journalsservice/views.py index 4fc862b..74bc4e3 100644 --- a/journalsservice/views.py +++ b/journalsservice/views.py @@ -108,15 +108,24 @@ def get(self, bibstem, volume): try: q = ADSQuery() solr_result = q.search(bibstem, volume) - data = solr_result['response'] - count = data['numFound'] - volume = data['docs'][0]['volume'] - bibstem = data['docs'][0]['bibstem'][0] - holdings = [{'esources': rec['esources'], 'page': rec['page'][0]} for rec in data['docs']] - result = {'bibstem': bibstem, - 'volume': volume, - 'numFound': count, - 'holdings': holdings} + data = solr_result.get('response', None) + if data: + count = data.get('numFound', 0) + docs = data.get('docs', []) + if docs: + volume = docs[0].get('volume', None) + bibstem = docs[0].get('bibstem', []) + if bibstem: + bibstem = bibstem[0] + holdings = [{'esources': rec.get('esources', None), 'page': rec.get('page', [None])[0]} for rec in docs] + result = {'bibstem': bibstem, + 'volume': volume, + 'numFound': count, + 'holdings': holdings} + else: + result = {} + else: + result = {} except Exception as err: return {'Error': 'Holdings search failed', 'Error Info': str(err)}, 500