Skip to content

Commit b566e69

Browse files
committed
fix: improve export empty chat excel error info
1 parent 632fad4 commit b566e69

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

backend/apps/chat/api/chat.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
delete_chat, get_chat_chart_data, get_chat_predict_data, get_chat_with_records_with_data, get_chat_record_by_id
1414
from apps.chat.models.chat_model import CreateChat, ChatRecord, RenameChat, ChatQuestion, ExcelData
1515
from apps.chat.task.llm import LLMService
16-
from common.core.deps import CurrentAssistant, SessionDep, CurrentUser
16+
from common.core.deps import CurrentAssistant, SessionDep, CurrentUser, Trans
1717

1818
router = APIRouter(tags=["Data Q&A"], prefix="/chat")
1919

@@ -106,7 +106,7 @@ async def start_chat(session: SessionDep, current_user: CurrentUser):
106106
async def recommend_questions(session: SessionDep, current_user: CurrentUser, chat_record_id: int,
107107
current_assistant: CurrentAssistant):
108108
def _return_empty():
109-
yield 'data:' + orjson.dumps( {'content': '[]', 'type': 'recommended_question'}).decode() + '\n\n'
109+
yield 'data:' + orjson.dumps({'content': '[]', 'type': 'recommended_question'}).decode() + '\n\n'
110110

111111
try:
112112
record = get_chat_record_by_id(session, chat_record_id)
@@ -201,10 +201,16 @@ def _err(_e: Exception):
201201

202202

203203
@router.post("/excel/export")
204-
async def export_excel(excel_data: ExcelData):
204+
async def export_excel(excel_data: ExcelData, trans: Trans):
205205
def inner():
206206
_fields_list = []
207207
data = []
208+
if not excel_data.data:
209+
raise HTTPException(
210+
status_code=500,
211+
detail=trans("i18n_excel_export.data_is_empty")
212+
)
213+
208214
for _data in excel_data.data:
209215
_row = []
210216
for field in excel_data.axis:

backend/locales/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@
4747
"datasource_cannot_be_none": "Datasource cannot be none",
4848
"data_training_not_exists": "Example does not exists",
4949
"exists_in_db": "Question exists"
50+
},
51+
"i18n_excel_export": {
52+
"data_is_empty": "The form data is empty, cannot export data"
5053
}
5154
}

backend/locales/zh-CN.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@
4747
"datasource_cannot_be_none": "数据源不能为空",
4848
"data_training_not_exists": "该示例不存在",
4949
"exists_in_db": "该问题已存在"
50+
},
51+
"i18n_excel_export": {
52+
"data_is_empty": "表单数据为空,无法导出数据"
5053
}
5154
}

frontend/src/api/chat.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,9 @@ export const chatApi = {
332332
return request.fetchStream(`/chat/recommend_questions/${record_id}`, {}, controller)
333333
},
334334
checkLLMModel: () => request.get('/system/aimodel/default', { requestOptions: { silent: true } }),
335-
export2Excel: (data: any) => request.post('/chat/excel/export', data, { responseType: 'blob' }),
335+
export2Excel: (data: any) =>
336+
request.post('/chat/excel/export', data, {
337+
responseType: 'blob',
338+
requestOptions: { customError: true },
339+
}),
336340
}

frontend/src/views/chat/chat-block/ChartBlock.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,31 @@ function exportToExcel() {
255255
link.click()
256256
document.body.removeChild(link)
257257
})
258+
.catch(async (error) => {
259+
if (error.response) {
260+
try {
261+
let text = await error.response.data.text()
262+
try {
263+
text = JSON.parse(text)
264+
} finally {
265+
ElMessage({
266+
message: text,
267+
type: 'error',
268+
showClose: true,
269+
})
270+
}
271+
} catch (e) {
272+
console.error('Error processing error response:', e)
273+
}
274+
} else {
275+
console.error('Other error:', error)
276+
ElMessage({
277+
message: error,
278+
type: 'error',
279+
showClose: true,
280+
})
281+
}
282+
})
258283
.finally(() => {
259284
loading.value = false
260285
})

0 commit comments

Comments
 (0)