forked from ScoDoc/DocScoDoc
Portal requests: better log messages on errors
This commit is contained in:
parent
123af7067c
commit
58e42c093d
@ -360,7 +360,7 @@ def find_new_dir():
|
||||
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.
|
||||
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)
|
||||
url = photo_portal_url(etud)
|
||||
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")
|
||||
f = None
|
||||
error_message = None
|
||||
try:
|
||||
log(f"copy_portal_photo_to_fs: getting {url}")
|
||||
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("called from:\n" + "".join(traceback.format_stack()))
|
||||
log("copy_portal_photo_to_fs: error.")
|
||||
return None, "%s: erreur chargement de %s" % (etud["nomprenom"], url)
|
||||
log("copy_portal_photo_to_fs: unknown requests error.")
|
||||
if error_message is not None:
|
||||
return (
|
||||
None,
|
||||
f"""{etud["nomprenom"]}: erreur chargement de {url}\n{error_message}""",
|
||||
)
|
||||
if r.status_code != 200:
|
||||
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
|
||||
try:
|
||||
status, err_msg = store_photo(etud, data, "(inconnue)")
|
||||
status, error_message = store_photo(etud, data, "(inconnue)")
|
||||
except Exception:
|
||||
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)!")
|
||||
if status:
|
||||
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",
|
||||
)
|
||||
else:
|
||||
return None, "%s: <b>%s</b>" % (etud["nomprenom"], err_msg)
|
||||
return None, f"{etud['nomprenom']}: <b>{error_message}</b>"
|
||||
|
@ -1042,15 +1042,23 @@ def query_portal(req, msg="Portail Apogee", timeout=3):
|
||||
returns a string, "" on error
|
||||
"""
|
||||
log("query_portal: %s" % req)
|
||||
|
||||
error_message = None
|
||||
try:
|
||||
r = requests.get(req, timeout=timeout) # seconds / request
|
||||
except:
|
||||
log("query_portal: can't connect to %s" % msg)
|
||||
except requests.ConnectionError:
|
||||
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 ""
|
||||
if r.status_code != 200:
|
||||
log(f"query_portal: http error {r.status_code}")
|
||||
return "" # XXX ou raise exception ?
|
||||
return ""
|
||||
|
||||
return r.text
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user