mirror of
https://github.com/rembo10/headphones.git
synced 2026-03-31 10:09:28 +01:00
Strip message for debug logging. Truncated lines
This commit is contained in:
@@ -22,7 +22,8 @@ import requests
|
||||
import feedparser
|
||||
import headphones
|
||||
|
||||
def request_response(url, method="get", auto_raise=True, whitelist_status_code=None, **kwargs):
|
||||
def request_response(url, method="get", auto_raise=True,
|
||||
whitelist_status_code=None, **kwargs):
|
||||
"""
|
||||
Convenient wrapper for `requests.get', which will capture the exceptions and
|
||||
log them. On success, the Response object is returned. In case of a
|
||||
@@ -53,16 +54,19 @@ def request_response(url, method="get", auto_raise=True, whitelist_status_code=N
|
||||
try:
|
||||
response.raise_for_status()
|
||||
except:
|
||||
logger.debug("Response status code %d is not white listed, raised exception", response.status_code)
|
||||
logger.debug("Response status code %d is not white " +
|
||||
"listed, raised exception", response.status_code)
|
||||
raise
|
||||
elif auto_raise:
|
||||
response.raise_for_status()
|
||||
|
||||
return response
|
||||
except requests.ConnectionError:
|
||||
logger.error("Unable to connect to remote host. Check if the remote host is up and running.")
|
||||
logger.error("Unable to connect to remote host. Check if the remote " +
|
||||
"host is up and running.")
|
||||
except requests.Timeout:
|
||||
logger.error("Request timed out. The remote host did not respeond timely.")
|
||||
logger.error("Request timed out. The remote host did not respeond " +
|
||||
"timely.")
|
||||
except requests.HTTPError as e:
|
||||
if e.response is not None:
|
||||
if e.response.status_code >= 500:
|
||||
@@ -73,24 +77,26 @@ def request_response(url, method="get", auto_raise=True, whitelist_status_code=N
|
||||
# I don't think we will end up here, but for completeness
|
||||
cause = "unknown"
|
||||
|
||||
logger.error("Request raise HTTP error with status code %d (%s).", e.response.status_code, cause)
|
||||
logger.error("Request raise HTTP error with status code %d (%s).",
|
||||
e.response.status_code, cause)
|
||||
|
||||
# Some servers return extra information in the result. Try to parse
|
||||
# it for debugging purpose. Messages are limited to 100 characters,
|
||||
# it for debugging purpose. Messages are limited to 150 characters,
|
||||
# since it may return the whole page in case of normal web page URLs
|
||||
if headphones.VERBOSE:
|
||||
if e.response.headers.get('content-type') == 'text/html':
|
||||
if e.response.headers.get("content-type") == "text/html":
|
||||
soup = BeautifulSoup(e.response.content, "html5lib")
|
||||
|
||||
message = soup.find("body")
|
||||
message = message.text if message else soup.text
|
||||
message = message.strip()
|
||||
else:
|
||||
message = e.response.content
|
||||
message = e.response.content.strip()
|
||||
|
||||
if message:
|
||||
# Truncate message if it is too long.
|
||||
if len(message) > 100:
|
||||
message = message[:100] + "..."
|
||||
if len(message) > 150:
|
||||
message = message[:150] + "..."
|
||||
|
||||
logger.debug("Server responded with message: %s", message)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user