fix: create_apikey.dspy 添加重复key返回500,加try/except

添加重复的应用ID或名称时,sor.C()抛Duplicate异常未捕获,
导致500错误而非友好的提示信息。

修复:
- 包裹主逻辑在try/except中
- Duplicate/1062错误: 返回'应用ID或名称已存在'
- 其他错误: 返回具体错误信息
This commit is contained in:
yumoqing 2026-06-29 14:46:33 +08:00
parent 5b69dd62b5
commit af142173bb
2 changed files with 30 additions and 22 deletions

Binary file not shown.

View File

@ -20,6 +20,7 @@ kw = {
]
}
try:
async with db.sqlorContext(dbname) as sor:
orgid = await get_userorgid()
userid = await get_user()
@ -42,4 +43,11 @@ async with db.sqlorContext(dbname) as sor:
}
await sor.C('downapikey', ns1)
return UiMessage(title="create apikey", message=f"apikey created", **kw)
except Exception as e:
err_msg = str(e)
debug(f'create_apikey error: {format_exc()}')
if 'Duplicate' in err_msg or '1062' in err_msg:
return UiError(title='创建失败', message='应用ID或名称已存在请使用不同的ID或名称')
return UiError(title='创建失败', message=f'创建API Key失败: {err_msg}')
return UiError(title='create apikey', message='add apikey error')