Portal requests: better log messages on errors

This commit is contained in:
Emmanuel Viennet 2022-09-14 10:03:08 +02:00
parent 123af7067c
commit 58e42c093d
2 changed files with 36 additions and 14 deletions

View File

@ -360,7 +360,7 @@ def find_new_dir():
return d + "/" return d + "/"
def copy_portal_photo_to_fs(etud): def copy_portal_photo_to_fs(etud: dict):
"""Copy the photo from portal (distant website) to local fs. """Copy the photo from portal (distant website) to local fs.
Returns rel. path or None if copy failed, with a diagnostic message Returns rel. path or None if copy failed, with a diagnostic message
""" """
@ -368,26 +368,40 @@ def copy_portal_photo_to_fs(etud):
sco_etud.format_etud_ident(etud) sco_etud.format_etud_ident(etud)
url = photo_portal_url(etud) url = photo_portal_url(etud)
if not url: if not url:
return None, "%(nomprenom)s: pas de code NIP" % etud return None, f"""{etud['nomprenom']}: pas de code NIP"""
portal_timeout = sco_preferences.get_preference("portal_timeout") portal_timeout = sco_preferences.get_preference("portal_timeout")
f = None error_message = None
try: try:
log(f"copy_portal_photo_to_fs: getting {url}") log(f"copy_portal_photo_to_fs: getting {url}")
r = requests.get(url, timeout=portal_timeout) r = requests.get(url, timeout=portal_timeout)
except Exception: except requests.ConnectionError:
log("copy_portal_photo_to_fs: ConnectionError.")
error_message = "ConnectionError"
except requests.Timeout:
log("copy_portal_photo_to_fs: Timeout.")
error_message = "Timeout"
except requests.TooManyRedirects:
log("copy_portal_photo_to_fs: TooManyRedirects.")
error_message = "TooManyRedirects"
except requests.RequestException:
# log("download failed: exception:\n%s" % traceback.format_exc()) # log("download failed: exception:\n%s" % traceback.format_exc())
# log("called from:\n" + "".join(traceback.format_stack())) # log("called from:\n" + "".join(traceback.format_stack()))
log("copy_portal_photo_to_fs: error.") log("copy_portal_photo_to_fs: unknown requests error.")
return None, "%s: erreur chargement de %s" % (etud["nomprenom"], url) if error_message is not None:
return (
None,
f"""{etud["nomprenom"]}: erreur chargement de {url}\n{error_message}""",
)
if r.status_code != 200: if r.status_code != 200:
log(f"copy_portal_photo_to_fs: download failed {r.status_code }") log(f"copy_portal_photo_to_fs: download failed {r.status_code }")
return None, "%s: erreur chargement de %s" % (etud["nomprenom"], url) return None, f"""{etud["nomprenom"]}: erreur chargement de {url}"""
data = r.content # image bytes data = r.content # image bytes
try: try:
status, err_msg = store_photo(etud, data, "(inconnue)") status, error_message = store_photo(etud, data, "(inconnue)")
except Exception: except Exception:
status = False status = False
err_msg = "Erreur chargement photo du portail" error_message = "Erreur chargement photo du portail"
log("copy_portal_photo_to_fs: failure (exception in store_photo)!") log("copy_portal_photo_to_fs: failure (exception in store_photo)!")
if status: if status:
log(f"copy_portal_photo_to_fs: copied {url}") log(f"copy_portal_photo_to_fs: copied {url}")
@ -396,4 +410,4 @@ def copy_portal_photo_to_fs(etud):
f"{etud['nomprenom']}: photo chargée", f"{etud['nomprenom']}: photo chargée",
) )
else: else:
return None, "%s: <b>%s</b>" % (etud["nomprenom"], err_msg) return None, f"{etud['nomprenom']}: <b>{error_message}</b>"

View File

@ -1042,15 +1042,23 @@ def query_portal(req, msg="Portail Apogee", timeout=3):
returns a string, "" on error returns a string, "" on error
""" """
log("query_portal: %s" % req) log("query_portal: %s" % req)
error_message = None
try: try:
r = requests.get(req, timeout=timeout) # seconds / request r = requests.get(req, timeout=timeout) # seconds / request
except: except requests.ConnectionError:
log("query_portal: can't connect to %s" % msg) error_message = "ConnectionError"
except requests.Timeout:
error_message = "Timeout"
except requests.TooManyRedirects:
error_message = "TooManyRedirects"
except requests.RequestException:
error_message = "can't connect to {msg}"
if error_message is not None:
log(f"query_portal: {error_message}")
return "" return ""
if r.status_code != 200: if r.status_code != 200:
log(f"query_portal: http error {r.status_code}") log(f"query_portal: http error {r.status_code}")
return "" # XXX ou raise exception ? return ""
return r.text return r.text