mirror of
https://github.com/rembo10/headphones.git
synced 2026-05-02 09:49:36 +01:00
Strip message for debug logging. Truncated lines
This commit is contained in:
@@ -22,7 +22,8 @@ import requests
|
|||||||
import feedparser
|
import feedparser
|
||||||
import headphones
|
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
|
Convenient wrapper for `requests.get', which will capture the exceptions and
|
||||||
log them. On success, the Response object is returned. In case of a
|
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:
|
try:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except:
|
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
|
raise
|
||||||
elif auto_raise:
|
elif auto_raise:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
return response
|
return response
|
||||||
except requests.ConnectionError:
|
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:
|
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:
|
except requests.HTTPError as e:
|
||||||
if e.response is not None:
|
if e.response is not None:
|
||||||
if e.response.status_code >= 500:
|
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
|
# I don't think we will end up here, but for completeness
|
||||||
cause = "unknown"
|
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
|
# 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
|
# since it may return the whole page in case of normal web page URLs
|
||||||
if headphones.VERBOSE:
|
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")
|
soup = BeautifulSoup(e.response.content, "html5lib")
|
||||||
|
|
||||||
message = soup.find("body")
|
message = soup.find("body")
|
||||||
message = message.text if message else soup.text
|
message = message.text if message else soup.text
|
||||||
|
message = message.strip()
|
||||||
else:
|
else:
|
||||||
message = e.response.content
|
message = e.response.content.strip()
|
||||||
|
|
||||||
if message:
|
if message:
|
||||||
# Truncate message if it is too long.
|
# Truncate message if it is too long.
|
||||||
if len(message) > 100:
|
if len(message) > 150:
|
||||||
message = message[:100] + "..."
|
message = message[:150] + "..."
|
||||||
|
|
||||||
logger.debug("Server responded with message: %s", message)
|
logger.debug("Server responded with message: %s", message)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user