From e3b3b5a1f656cd360c5ee3f090aeed6acfede446 Mon Sep 17 00:00:00 2001 From: mxr612 Date: Mon, 16 Jun 2025 06:29:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BB=A5=E5=A2=9E=E5=BC=BA=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E4=BF=9D=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在结果处理逻辑中添加try-except块,以捕获保存用户响应到数据库时的异常 - 确保在发生错误时能够打印异常信息,提升系统的稳定性和可调试性 --- app.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index 7ba1d72..234fc62 100644 --- a/app.py +++ b/app.py @@ -120,22 +120,25 @@ async def result(request: Request, scale_id: str, db: Session = Depends(get_db)) else: responses[subscale] += int(form_data[str(qid)]) average[subscale] = round(responses[subscale]/len(qids),2) - # Save response to database - ip = request.headers.get("X-Forwarded-For", "").split(",")[0].strip() or \ - request.headers.get("X-Real-IP", "") or \ - request.client.host # Get real IP address considering proxy headers - location = get_location_from_ip(ip)# Get location information - db_response = RawResponse( - scale_id=scale_id, - user_agent=request.headers.get("user-agent", "Unknown"), - ip_address=ip, - location=json.dumps(location) if location else None, - raw_response=dict(form_data), - sum_response=responses, - avg_response=average - ) - db.add(db_response) - db.commit() + try: + # Save response to database + ip = request.headers.get("X-Forwarded-For", "").split(",")[0].strip() or \ + request.headers.get("X-Real-IP", "") or \ + request.client.host # Get real IP address considering proxy headers + location = get_location_from_ip(ip)# Get location information + db_response = RawResponse( + scale_id=scale_id, + user_agent=request.headers.get("user-agent", "Unknown"), + ip_address=ip, + location=json.dumps(location) if location else None, + raw_response=dict(form_data), + sum_response=responses, + avg_response=average + ) + db.add(db_response) + db.commit() + except Exception as e: + print(e) return templates.TemplateResponse("result.html", { "request": request, "responses": responses,