Skip to content

Commit a65399f

Browse files
committed
logging: better message for HTTP status != 200
We already extract error message from Solr responses and that is great. Unfortunately it can contain the data that may change with every request (like document id). This creates an issue when user uses Sentry or other solution that captures logging or exceptions. Previous implementation causes many duplicated events in Sentry if message extracted using `self._extract_error(resp)` contained such variable data. This change uses 'non-mutable' message that is complemented with extracted data that using string formatting option supplied by Python logging. Thanks to this, Sentry and other solutions can perform better grouping of loging messages (by status code). This is approach that is already used in handling other errors.
1 parent bf6d8b6 commit a65399f

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ Contributors:
3333
* Karol Sikora (@sicarrots) for Solr 4 softCommit support
3434
* Çağatay Çallı (@faraday) for Solr 4 field update support
3535
* Emmanuel Leblond (@touilleMan) for fixing error handling on Python 3
36+
* Michał Jaworski (@swistakm) for improved Sentry-friendly logging

pysolr.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,12 @@ def _send_request(self, method, path='', body=None, headers=None, files=None):
386386
url, method, log_body[:10], end_time - start_time)
387387

388388
if int(resp.status_code) != 200:
389-
error_message = self._extract_error(resp)
390-
self.log.error(error_message, extra={'data': {'headers': resp.headers,
391-
'response': resp.content}})
392-
raise SolrError(error_message)
389+
error_message = "Solr responded with an error (HTTP %s): %s"
390+
solr_message = self._extract_error(resp)
391+
self.log.error(error_message, resp.status_code, solr_message,
392+
extra={'data': {'headers': resp.headers,
393+
'response': resp.content}})
394+
raise SolrError(error_message % (resp.status_code, solr_message))
393395

394396
return force_unicode(resp.content)
395397

0 commit comments

Comments
 (0)