Log request exceptions

This commit is contained in:
Ivan Habunek 2023-11-21 10:07:36 +01:00
parent 9664d71b57
commit 57be6beae8
No known key found for this signature in database
GPG key ID: F5F0623FF5EBCB3D
2 changed files with 7 additions and 2 deletions

View file

@ -3,7 +3,7 @@ from requests.exceptions import RequestException
from toot import __version__
from toot.exceptions import NotFoundError, ApiError
from toot.logging import log_request, log_response
from toot.logging import log_request, log_request_exception, log_response
def send_request(request, allow_redirects=True):
@ -19,6 +19,7 @@ def send_request(request, allow_redirects=True):
settings = session.merge_environment_settings(prepared.url, {}, None, None, None)
response = session.send(prepared, allow_redirects=allow_redirects, **settings)
except RequestException as ex:
log_request_exception(request, ex)
raise ApiError(f"Request failed: {str(ex)}")
log_response(response)

View file

@ -2,7 +2,7 @@ import json
import sys
from logging import getLogger
from requests import Request, Response
from requests import Request, RequestException, Response
from urllib.parse import urlencode
logger = getLogger("toot")
@ -56,6 +56,10 @@ def log_response(response: Response):
logger.debug(f" <-- {content}")
def log_request_exception(request: Request, ex: RequestException):
logger.debug(f" <-- {request.method} {_url(request)} Exception: {ex}")
def _url(request):
url = request.url
if request.params: