refactor: 统一业务异常处理,标准化结果码和错误响应

1.  新增并完善ResultCode枚举,补充标准HTTP状态码对应的业务状态码
2.  重构BusinessException,新增基于ResultCode的构造函数和ThrowIf扩展方法
3.  替换所有硬编码的HTTP状态码为统一的ResultCode枚举
4.  优化全局异常中间件,根据业务状态码映射对应HTTP状态码并规范化JSON响应
5.  修复OssImageHelper和AutoDotCodeConsumer中的OSS文件处理逻辑
6.  新增用户答题快照实体类
7.  清理废弃的宠物模块迁移脚本
This commit is contained in:
glz
2026-06-29 16:34:26 +08:00
parent 4579fefef9
commit bdaa6a0dc8
35 changed files with 693 additions and 364 deletions

View File

@ -25,24 +25,24 @@ public class AiBasePromptService(
if (string.IsNullOrWhiteSpace(input.PromptKey))
{
throw new BusinessException("配置标识不能为空", 400);
throw new BusinessException("配置标识不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(input.PromptName))
{
throw new BusinessException("配置名称不能为空", 400);
throw new BusinessException("配置名称不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(input.PromptTemplate))
{
throw new BusinessException("Prompt模板内容不能为空", 400);
throw new BusinessException("Prompt模板内容不能为空", ResultCode.BAD_REQUEST);
}
// 检查PromptKey是否已存在
var exists = await promptRepository.IsAnyAsync(p => p.PromptKey == input.PromptKey.Trim());
if (exists)
{
throw new BusinessException($"配置标识 '{input.PromptKey.Trim()}' 已存在", 400);
throw new BusinessException($"配置标识 '{input.PromptKey.Trim()}' 已存在", ResultCode.BAD_REQUEST);
}
var entity = new AiBasePrompt
@ -64,7 +64,7 @@ public class AiBasePromptService(
if (!result)
{
logger.LogError("Prompt配置创建失败PromptKey: {PromptKey}", input.PromptKey);
throw new BusinessException("创建Prompt配置失败", 500);
throw new BusinessException("创建Prompt配置失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("Prompt配置创建成功PromptKey: {PromptKey}, ID: {Id}", input.PromptKey, entity.Id);
@ -97,29 +97,29 @@ public class AiBasePromptService(
if (entity == null)
{
logger.LogWarning("未找到要更新的Prompt配置ID: {Id}", id);
throw new BusinessException("Prompt配置不存在", 404);
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
}
if (string.IsNullOrWhiteSpace(input.PromptKey))
{
throw new BusinessException("配置标识不能为空", 400);
throw new BusinessException("配置标识不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(input.PromptName))
{
throw new BusinessException("配置名称不能为空", 400);
throw new BusinessException("配置名称不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(input.PromptTemplate))
{
throw new BusinessException("Prompt模板内容不能为空", 400);
throw new BusinessException("Prompt模板内容不能为空", ResultCode.BAD_REQUEST);
}
// 检查PromptKey是否被其他记录占用
var exists = await promptRepository.IsAnyAsync(p => p.PromptKey == input.PromptKey.Trim() && p.Id != id);
if (exists)
{
throw new BusinessException($"配置标识 '{input.PromptKey.Trim()}' 已被其他配置使用", 400);
throw new BusinessException($"配置标识 '{input.PromptKey.Trim()}' 已被其他配置使用", ResultCode.BAD_REQUEST);
}
entity.PromptKey = input.PromptKey.Trim();
@ -135,7 +135,7 @@ public class AiBasePromptService(
if (!result)
{
logger.LogError("Prompt配置更新失败ID: {Id}", id);
throw new BusinessException("更新Prompt配置失败", 500);
throw new BusinessException("更新Prompt配置失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("Prompt配置更新成功ID: {Id}", id);
@ -168,14 +168,14 @@ public class AiBasePromptService(
if (entity == null)
{
logger.LogWarning("未找到要删除的Prompt配置ID: {Id}", id);
throw new BusinessException("Prompt配置不存在", 404);
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
}
var result = await promptRepository.DeleteByIdAsync(id);
if (!result)
{
logger.LogError("Prompt配置删除失败ID: {Id}", id);
throw new BusinessException("删除Prompt配置失败", 500);
throw new BusinessException("删除Prompt配置失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("Prompt配置删除成功ID: {Id}", id);
@ -192,7 +192,7 @@ public class AiBasePromptService(
if (entity == null)
{
logger.LogWarning("未找到Prompt配置ID: {Id}", id);
throw new BusinessException("Prompt配置不存在", 404);
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
}
return new AiBasePromptOutput
@ -221,12 +221,12 @@ public class AiBasePromptService(
if (input.PageIndex <= 0)
{
throw new BusinessException("页码必须大于0", 400);
throw new BusinessException("页码必须大于0", ResultCode.BAD_REQUEST);
}
if (input.PageSize <= 0 || input.PageSize > 100)
{
throw new BusinessException("每页条数必须在1-100之间", 400);
throw new BusinessException("每页条数必须在1-100之间", ResultCode.BAD_REQUEST);
}
RefAsync<int> totalNumber = 0;
@ -296,7 +296,7 @@ public class AiBasePromptService(
if (entity == null)
{
logger.LogWarning("未找到要切换状态的Prompt配置ID: {Id}", id);
throw new BusinessException("Prompt配置不存在", 404);
throw new BusinessException("Prompt配置不存在", ResultCode.NOT_FOUND);
}
entity.Status = entity.Status == 1 ? 0 : 1;
@ -307,7 +307,7 @@ public class AiBasePromptService(
if (!result)
{
logger.LogError("Prompt配置状态切换失败ID: {Id}", id);
throw new BusinessException("切换Prompt状态失败", 500);
throw new BusinessException("切换Prompt状态失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("Prompt配置状态切换成功ID: {Id}, Status: {Status}", id, entity.Status);