Skip to content

Commit

Permalink
Improve error reporting when upstream API is defunct with e.g. "Servi…
Browse files Browse the repository at this point in the history
…ce Unavailable: Back-end server is at capacity, see #5
  • Loading branch information
amotl committed Oct 3, 2018
1 parent cbacf54 commit b569029
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ USPTO Open Data API client CHANGES
development
===========
- Upgrade to lxml 4.2.5 to satisfy libxml2 compilation on recent Homebrew
- Improve error reporting when upstream API is defunct
with e.g. ``Service Unavailable: Back-end server is at capacity``. Thanks, Mohamed!

2017-11-20 0.7.4
================
Expand Down
21 changes: 16 additions & 5 deletions uspto/util/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,34 @@ def query(self, expression, filter=None, sort=None, start=None, rows=None, defau

# Submit the query to the Solr search service
response = self.session.post(self.QUERY_URL, json=solr_query)
#print(response.text)
#print(self.QUERY_URL, response.text)

# Check response and scrape appropriate error message from HTML on failure
if response.status_code != 200:
logger.error('Error while querying for %s\n%s', expression, response.text)
if response.headers['Content-Type'].startswith('text/html'):

# First error message
message = u'Error while querying for {}'.format(expression)
if response.text:
message += u'\n{}'.format(response.text)
logger.error(message)

# Compute second error message to reveal more details
if 'Content-Type' in response.headers and response.headers['Content-Type'].startswith('text/html'):
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('title')
message = title.string.strip()
hr = soup.body.find('hr')
reason = hr.next_sibling.string.strip()
if reason:
message += '. ' + reason
message += u'. ' + reason
message += ' (status={})'.format(response.status_code)
logger.error(message)
raise ValueError(message)
else:
raise ValueError('Search error with response of unknown Content-Type')
message = u'API error. Status: {}, Reason: {}, Content-Type: {}'.format(
response.status_code, response.reason, response.headers.get('Content-Type'))
logger.error(message)
raise ValueError(message)

return response.json()

Expand Down

0 comments on commit b569029

Please sign in to comment.