Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lending.py: Add logging to exceptions #3350

Merged
merged 9 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions openlibrary/core/lending.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def cached_work_authors_and_subjects(work_id):
get_work_authors_and_related_subjects, 'works_authors_and_subjects',
timeout=dateutil.HALF_DAY_SECS)(work_id)
except AttributeError:
logger.exception("cached_work_authors_and_subjects(%s)" % work_id)
return {'authors': [], 'subject': []}

@public
Expand Down Expand Up @@ -187,14 +188,15 @@ def compose_ia_url(limit=None, page=1, subject=None, query=None, work_id=None,
def get_random_available_ia_edition():
"""uses archive advancedsearch to raise a random book"""
try:
url=("http://%s/advancedsearch.php?q=_exists_:openlibrary_work"\
"+AND+loans__status__status:AVAILABLE"\
"&fl=identifier,openlibrary_edition,loans__status__status"\
"&output=json&rows=1&sort[]=random" % (config_bookreader_host))
url = ("http://%s/advancedsearch.php?q=_exists_:openlibrary_work"
"+AND+loans__status__status:AVAILABLE"
"&fl=identifier,openlibrary_edition,loans__status__status"
"&output=json&rows=1&sort[]=random" % (config_bookreader_host))
content = urllib.request.urlopen(url=url, timeout=config_http_request_timeout).read()
items = simplejson.loads(content).get('response', {}).get('docs', [])
return items[0]["openlibrary_edition"]
except Exception as e:
except Exception: # TODO: Narrow exception scope
logger.exception("get_random_available_ia_edition(%s)" % url)
return None

def get_available(limit=None, page=1, subject=None, query=None,
Expand Down Expand Up @@ -227,7 +229,8 @@ def get_available(limit=None, page=1, subject=None, query=None,
results[item['openlibrary_work']] = item['openlibrary_edition']
books = web.ctx.site.get_many(['/books/%s' % result for result in results.values()])
return books
except Exception as e:
except Exception: # TODO: Narrow exception scope
logger.exception("get_available(%s)" % url)
return {'error': 'request_timeout'}


Expand All @@ -241,7 +244,8 @@ def get_availability(key, ids):
try:
content = urllib.request.urlopen(url=url, timeout=config_http_request_timeout).read()
return simplejson.loads(content).get('responses', {})
except Exception as e:
except Exception as e: # TODO: Narrow exception scope
logger.exception("get_availability(%s)" % url)
return {'error': 'request_timeout', 'details': str(e)}

def get_edition_availability(ol_edition_id):
Expand Down Expand Up @@ -343,7 +347,8 @@ def is_loaned_out_on_ia(identifier):
try:
response = simplejson.loads(urllib.request.urlopen(url).read())
return response and response.get('checkedout')
except:
except Exception: # TODO: Narrow exception scope
logger.exception("is_loaned_out_on_ia(%s)" % identifier)
return None


Expand Down Expand Up @@ -375,13 +380,13 @@ def get_loan(identifier, user_key=None):
return loan.delete()
try:
_loan = _get_ia_loan(identifier, account and userkey2userid(account.username))
except Exception as e:
pass
except Exception: # TODO: Narrow exception scope
logger.exception("get_loan(%s) 1 of 2" % identifier)

try:
_loan = _get_ia_loan(identifier, account and account.itemname)
except Exception as e:
pass
except Exception: # TODO: Narrow exception scope
logger.exception("get_loan(%s) 2 of 2" % identifier)

return _loan

Expand Down Expand Up @@ -473,11 +478,11 @@ def sync_loan(identifier, loan=NOT_INITIALIZED):
}
try:
ebook.update(**kwargs)
except Exception:
except Exception: # TODO: Narrow exception scope
# updating ebook document is sometimes failing with
# "Document update conflict" error.
# Log the error in such cases, don't crash.
logger.error("failed to update ebook for %s", identifier, exc_info=True)
logger.exception("failed to update ebook for %s", identifier)

# fire loan-completed event
if is_loan_completed and ebook.get('loan'):
Expand Down Expand Up @@ -716,7 +721,7 @@ def get_data(self):
try:
return simplejson.loads(urllib.request.urlopen(url).read())
except IOError:
logger.error("unable to conact BSS server", exc_info=True)
logger.exception("unable to conact BSS server")

def has_loan(self):
return bool(self.get_loan())
Expand Down Expand Up @@ -805,7 +810,7 @@ def _post(self, **params):
timeout=config_http_request_timeout).read()
logger.info("POST response: %s", jsontext)
return simplejson.loads(jsontext)
except Exception as e:
except Exception: # TODO: Narrow exception scope
logger.exception("POST failed")
raise

Expand Down
4 changes: 2 additions & 2 deletions scripts/test-py3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RETURN_CODE=$?
pytest openlibrary/catalog/marc/tests/test_get_subjects.py || true
pytest openlibrary/catalog/marc/tests/test_parse.py || true
pytest openlibrary/tests/catalog/test_get_ia.py || true
pytest openlibrary/coverstore/tests/test_doctests.py || true
pytest openlibrary/plugins/openlibrary/tests/test_home.py || true
pytest --show-capture=all openlibrary/coverstore/tests/test_doctests.py || true
pytest --show-capture=all openlibrary/plugins/openlibrary/tests/test_home.py || true

exit ${RETURN_CODE}