Optimize api with semantic HTTP status codes (#681)

This commit is contained in:
Dylan
2025-06-23 22:18:17 +08:00
committed by GitHub
parent f9bfe8f510
commit 6d5e741d94
20 changed files with 91 additions and 62 deletions
@@ -43,7 +43,7 @@ class GenBusinessService:
async with async_db_session.begin() as db:
business = await gen_business_dao.get_by_name(db, obj.table_name)
if business:
raise errors.ForbiddenError(msg='代码生成业务已存在')
raise errors.ConflictError(msg='代码生成业务已存在')
await gen_business_dao.create(db, obj)
@staticmethod
@@ -76,7 +76,7 @@ class GenModelService:
if obj.name != model.name:
gen_models = await gen_model_dao.get_all_by_business(db, obj.gen_business_id)
if obj.name in [gen_model.name for gen_model in gen_models]:
raise errors.ForbiddenError(msg='模型列名已存在')
raise errors.ConflictError(msg='模型列名已存在')
pd_type = sql_type_to_pydantic(obj.type)
return await gen_model_dao.update(db, pk, obj, pd_type=pd_type)
@@ -55,7 +55,7 @@ class GenService:
business_info = await gen_business_dao.get_by_name(db, obj.table_name)
if business_info:
raise errors.ForbiddenError(msg='已存在相同数据库表业务')
raise errors.ConflictError(msg='已存在相同数据库表业务')
table_name = table_info[0]
new_business = GenBusiness(
@@ -52,10 +52,10 @@ class ConfigService:
"""
async with async_db_session.begin() as db:
if obj.type in settings.CONFIG_BUILT_IN_TYPES:
raise errors.ForbiddenError(msg='非法类型参数')
raise errors.RequestError(msg='非法类型参数')
config = await config_dao.get_by_key(db, obj.key)
if config:
raise errors.ForbiddenError(msg=f'参数配置 {obj.key} 已存在')
raise errors.ConflictError(msg=f'参数配置 {obj.key} 已存在')
await config_dao.create(db, obj)
@staticmethod
@@ -74,7 +74,7 @@ class ConfigService:
if config.key != obj.key:
config = await config_dao.get_by_key(db, obj.key)
if config:
raise errors.ForbiddenError(msg=f'参数配置 {obj.key} 已存在')
raise errors.ConflictError(msg=f'参数配置 {obj.key} 已存在')
count = await config_dao.update(db, pk, obj)
return count
@@ -58,7 +58,7 @@ class DictDataService:
async with async_db_session.begin() as db:
dict_data = await dict_data_dao.get_by_label(db, obj.label)
if dict_data:
raise errors.ForbiddenError(msg='字典数据已存在')
raise errors.ConflictError(msg='字典数据已存在')
dict_type = await dict_type_dao.get(db, obj.type_id)
if not dict_type:
raise errors.NotFoundError(msg='字典类型不存在')
@@ -79,7 +79,7 @@ class DictDataService:
raise errors.NotFoundError(msg='字典数据不存在')
if dict_data.label != obj.label:
if await dict_data_dao.get_by_label(db, obj.label):
raise errors.ForbiddenError(msg='字典数据已存在')
raise errors.ConflictError(msg='字典数据已存在')
dict_type = await dict_type_dao.get(db, obj.type_id)
if not dict_type:
raise errors.NotFoundError(msg='字典类型不存在')
@@ -49,7 +49,7 @@ class DictTypeService:
async with async_db_session.begin() as db:
dict_type = await dict_type_dao.get_by_code(db, obj.code)
if dict_type:
raise errors.ForbiddenError(msg='字典类型已存在')
raise errors.ConflictError(msg='字典类型已存在')
await dict_type_dao.create(db, obj)
@staticmethod
@@ -67,7 +67,7 @@ class DictTypeService:
raise errors.NotFoundError(msg='字典类型不存在')
if dict_type.code != obj.code:
if await dict_type_dao.get_by_code(db, obj.code):
raise errors.ForbiddenError(msg='字典类型已存在')
raise errors.ConflictError(msg='字典类型已存在')
count = await dict_type_dao.update(db, pk, obj)
return count
+1 -1
View File
@@ -331,4 +331,4 @@ class PluginStatusChecker:
log.error(f'插件 {self.plugin} 状态未初始化或丢失,需重启服务自动修复')
raise PluginInjectError(f'插件 {self.plugin} 状态未初始化或丢失,请联系系统管理员')
if not int(plugin_status.get(self.plugin)):
raise errors.ForbiddenError(msg=f'插件 {self.plugin} 未启用,请联系系统管理员')
raise errors.ServerError(msg=f'插件 {self.plugin} 未启用,请联系系统管理员')