baoyubo 1 mēnesi atpakaļ
vecāks
revīzija
014c9fcb87
1 mainītis faili ar 17 papildinājumiem un 4 dzēšanām
  1. 17 4
      routers/api/dataManagement/__init__.py

+ 17 - 4
routers/api/dataManagement/__init__.py

@@ -261,7 +261,9 @@ async def import_data(table_id: int, file: UploadFile = File(...), db: Session =
         workbook = openpyxl.load_workbook(file.file)
         sheet = workbook.active
     except Exception as e:
-        raise HTTPException(status_code=400, detail="Invalid Excel file")
+        traceback.print_exc()
+        return JSONResponse(status_code=400, content={'code': 400, 'msg': f"接口发生错误:{e}"})
+        # raise HTTPException(status_code=400, detail="Invalid Excel file")
 
         # 获取字段名和字段备注名
     column_names = [col["column_name"] for col in columns]
@@ -270,12 +272,19 @@ async def import_data(table_id: int, file: UploadFile = File(...), db: Session =
     # 检查第一行是否为字段备注名
     first_row = [cell.value for cell in sheet[1]]
     if first_row != column_comments:
-        raise HTTPException(status_code=400, detail="Excel columns do not match the expected columns")
+        print("接口发生错误:Excel columns do not match the expected columns")
+
+        return JSONResponse(status_code=400, content={'code': 400, 'msg': f"接口发生错误:Excel columns do not match the expected columns"})
+        # raise HTTPException(status_code=400, detail="Excel columns do not match the expected columns")
 
     # 检查第二行是否为字段名
     second_row = [cell.value for cell in sheet[2]]
     if second_row != column_names:
-        raise HTTPException(status_code=400, detail="Excel columns do not match the expected columns")
+        print("接口发生错误:Excel columns do not match the expected columns")
+        return JSONResponse(status_code=400,
+                            content={'code': 400, 'msg': f"接口发生错误:Excel columns do not match the expected columns"})
+
+        # raise HTTPException(status_code=400, detail="Excel columns do not match the expected columns")
 
     # 将数据插入到数据库
     try:
@@ -287,4 +296,8 @@ async def import_data(table_id: int, file: UploadFile = File(...), db: Session =
         return {"message": "Data imported successfully"}
     except Exception as e:
         db.rollback()
-        raise HTTPException(status_code=500, detail=str(e))
+        traceback.print_exc()
+        return JSONResponse(status_code=500,
+                            content={'code': 500, 'msg': f"接口发生错误:{e}"})
+
+        # raise HTTPException(status_code=500, detail=str(e))