1
0
forked from ScoDoc/ScoDoc

Exception speciale pour envoyer bug report

This commit is contained in:
Emmanuel Viennet 2022-03-21 11:48:34 +01:00
parent 9a13470ff3
commit 9032b1fa67
7 changed files with 65 additions and 35 deletions

View File

@ -10,6 +10,7 @@ import traceback
import logging
from logging.handlers import SMTPHandler, WatchedFileHandler
from threading import Thread
from flask import current_app, g, request
from flask import Flask
@ -27,6 +28,7 @@ import sqlalchemy
from app.scodoc.sco_exceptions import (
AccessDenied,
ScoBugCatcher,
ScoGenError,
ScoValueError,
APIInvalidParams,
@ -77,6 +79,28 @@ def internal_server_error(exc):
)
def handle_sco_bug(exc):
"""Un bug, en général rare, sur lequel les dev cherchent des
informations pour le corriger.
"""
Thread(
target=_async_dump, args=(current_app._get_current_object(), request.url)
).start()
return internal_server_error(exc)
def _async_dump(app, request_url: str):
from app.scodoc.sco_dump_db import sco_dump_and_send_db
with app.app_context():
ndb.open_db_connection()
try:
sco_dump_and_send_db("ScoBugCatcher", request_url=request_url)
except ScoValueError:
pass
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
@ -196,7 +220,7 @@ def create_app(config_class=DevConfig):
app.register_error_handler(ScoGenError, handle_sco_value_error)
app.register_error_handler(ScoValueError, handle_sco_value_error)
app.register_error_handler(ScoBugCatcher, handle_sco_bug)
app.register_error_handler(AccessDenied, handle_access_denied)
app.register_error_handler(500, internal_server_error)
app.register_error_handler(503, postgresql_server_error)

View File

@ -49,6 +49,7 @@ Balises img: actuellement interdites.
from reportlab.platypus import KeepTogether, Paragraph, Spacer, Table
from reportlab.lib.units import cm, mm
from reportlab.lib.colors import Color, blue
from app.scodoc.sco_exceptions import ScoBugCatcher
import app.scodoc.sco_utils as scu
from app.scodoc.sco_pdf import SU
@ -416,7 +417,11 @@ class BulletinGeneratorStandard(sco_bulletins_generator.BulletinGenerator):
# Chaque UE:
for ue in I["ues"]:
ue_type = None
coef_ue = ue["coef_ue_txt"] if prefs["bul_show_ue_coef"] else ""
try:
coef_ue = ue["coef_ue_txt"] if prefs["bul_show_ue_coef"] else ""
except TypeError as exc:
raise ScoBugCatcher(f"ue={ue!r}") from exc
ue_descr = ue["ue_descr_txt"]
rowstyle = ""
plusminus = minuslink #

View File

@ -51,14 +51,12 @@ import fcntl
import subprocess
import requests
from flask import flash, request
from flask import g, request
from flask_login import current_user
import app.scodoc.notesdb as ndb
import app.scodoc.sco_utils as scu
from app import log
from app.scodoc import html_sco_header
from app.scodoc import sco_preferences
from app.scodoc import sco_users
import sco_version
from app.scodoc.sco_exceptions import ScoValueError
@ -68,8 +66,7 @@ SCO_DUMP_LOCK = "/tmp/scodump.lock"
def sco_dump_and_send_db(message: str = "", request_url: str = ""):
"""Dump base de données et l'envoie anonymisée pour debug"""
H = [html_sco_header.sco_header(page_title="Assistance technique")]
# get currect (dept) DB name:
# get current (dept) DB name:
cursor = ndb.SimpleQuery("SELECT current_database()", {})
db_name = cursor.fetchone()[0]
ano_db_name = "ANO" + db_name
@ -96,27 +93,7 @@ def sco_dump_and_send_db(message: str = "", request_url: str = ""):
# Send
r = _send_db(ano_db_name, message, request_url)
if (
r.status_code
== requests.codes.INSUFFICIENT_STORAGE # pylint: disable=no-member
):
H.append(
"""<p class="warning">
Erreur: espace serveur trop plein.
Merci de contacter <a href="mailto:{0}">{0}</a></p>""".format(
scu.SCO_DEV_MAIL
)
)
elif r.status_code == requests.codes.OK: # pylint: disable=no-member
H.append("""<p>Opération effectuée.</p>""")
else:
H.append(
"""<p class="warning">
Erreur: code <tt>{0} {1}</tt>
Merci de contacter <a href="mailto:{2}">{2}</a></p>""".format(
r.status_code, r.reason, scu.SCO_DEV_MAIL
)
)
code = r.status_code
finally:
# Drop anonymized database
@ -125,8 +102,8 @@ def sco_dump_and_send_db(message: str = "", request_url: str = ""):
fcntl.flock(x, fcntl.LOCK_UN)
log("sco_dump_and_send_db: done.")
flash("Données envoyées au serveur d'assistance")
return "\n".join(H) + html_sco_header.sco_footer()
return code
def _duplicate_db(db_name, ano_db_name):
@ -195,7 +172,7 @@ def _send_db(ano_db_name: str, message: str = "", request_url: str = ""):
scu.SCO_DUMP_UP_URL,
files=files,
data={
"dept_name": sco_preferences.get_preference("DeptName"),
"dept_name": getattr(g, "scodoc_dept", "-"),
"message": message or "",
"request_url": request_url or request.url,
"serial": _get_scodoc_serial(),

View File

@ -47,9 +47,12 @@ class ScoValueError(ScoException):
self.dest_url = dest_url
class ScoBugCatcher(ScoException):
"bug avec enquete en cours"
class NoteProcessError(ScoValueError):
"Valeurs notes invalides"
pass
class InvalidEtudId(NoteProcessError):

View File

@ -73,6 +73,7 @@ from app.scodoc.scolog import logdb
from app.scodoc.sco_exceptions import (
AccessDenied,
ScoBugCatcher,
ScoException,
ScoValueError,
ScoInvalidIdType,

View File

@ -30,7 +30,7 @@ issu de ScoDoc7 / ZScolar.py
Emmanuel Viennet, 2021
"""
import os
import requests
import time
import flask
@ -2198,4 +2198,24 @@ def stat_bac(formsemestre_id):
@scodoc7func
def sco_dump_and_send_db(message="", request_url=""):
"Send anonymized data to supervision"
return sco_dump_db.sco_dump_and_send_db(message, request_url)
status_code = sco_dump_db.sco_dump_and_send_db(message, request_url)
H = [html_sco_header.sco_header(page_title="Assistance technique")]
if status_code == requests.codes.INSUFFICIENT_STORAGE: # pylint: disable=no-member
H.append(
"""<p class="warning">
Erreur: espace serveur trop plein.
Merci de contacter <a href="mailto:{0}">{0}</a></p>""".format(
scu.SCO_DEV_MAIL
)
)
elif status_code == requests.codes.OK: # pylint: disable=no-member
H.append("""<p>Opération effectuée.</p>""")
else:
H.append(
f"""<p class="warning">
Erreur: code <tt>{status_code}</tt>
Merci de contacter <a href="mailto:{scu.SCO_DEV_MAIL}">{scu.SCO_DEV_MAIL}</a></p>"""
)
flash("Données envoyées au serveur d'assistance")
return "\n".join(H) + html_sco_header.sco_footer()

View File

@ -1,7 +1,7 @@
# -*- mode: python -*-
# -*- coding: utf-8 -*-
SCOVERSION = "9.1.82"
SCOVERSION = "9.1.83"
SCONAME = "ScoDoc"