dapi/wwwroot/create_apikey.dspy
yumoqing af142173bb fix: create_apikey.dspy 添加重复key返回500,加try/except
添加重复的应用ID或名称时,sor.C()抛Duplicate异常未捕获,
导致500错误而非友好的提示信息。

修复:
- 包裹主逻辑在try/except中
- Duplicate/1062错误: 返回'应用ID或名称已存在'
- 其他错误: 返回具体错误信息
2026-06-29 14:46:33 +08:00

54 lines
1.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

debug(f'{params_kw=}')
dbname = get_module_dbname('dapi')
db = DBPools()
kw = {
"binds": [
{
"wid": "self",
"event": "dismissed",
"actiontype": "script",
"target": 'app.' + params_kw.data_widget,
"script": "this.render()"
},
{
"wid": "self",
"event": "dismissed",
"actiontype": "script",
"target": 'app.' + params_kw.referer_widget,
"script": "this.dismiss()"
}
]
}
try:
async with db.sqlorContext(dbname) as sor:
orgid = await get_userorgid()
userid = await get_user()
ns = {
"id": params_kw.id,
"name": params_kw.appname,
"description": params_kw.description,
"secretkey": password_encode(uuid()),
"allowedips": params_kw.allowedips,
"orgid": orgid
}
await sor.C('downapp', ns)
ns1 = {
"id": uuid(),
"dappid": ns['id'],
"userid": userid,
"apikey": password_encode(uuid()),
"enabled_date": curDateString(),
"expired_date": '9999-12-31'
}
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')