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

@ -27,12 +27,12 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (string.IsNullOrWhiteSpace(input.UserName))
{
throw new BusinessException("用户名不能为空", 400);
throw new BusinessException("用户名不能为空", ResultCode.BAD_REQUEST);
}
if (string.IsNullOrWhiteSpace(input.Password))
{
throw new BusinessException("密码不能为空", 400);
throw new BusinessException("密码不能为空", ResultCode.BAD_REQUEST);
}
// 检查用户名是否已存在
@ -40,7 +40,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (existingUser != null)
{
logger.LogWarning("创建管理员失败,用户名已存在: {UserName}", input.UserName);
throw new BusinessException("用户名已存在", 400);
throw new BusinessException("用户名已存在", ResultCode.CONFLICT);
}
var adminUser = new AdminUser
@ -60,7 +60,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (!result)
{
logger.LogError("管理员创建失败,用户名: {UserName}", input.UserName);
throw new BusinessException("创建管理员失败", 500);
throw new BusinessException("创建管理员失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("管理员创建成功,用户名: {UserName}, ID: {Id}", input.UserName, adminUser.Id);
@ -79,7 +79,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (adminUser == null)
{
logger.LogWarning("未找到要更新的管理员ID: {Id}", id);
throw new BusinessException("管理员不存在", 404);
throw new BusinessException("管理员不存在", ResultCode.NOT_FOUND);
}
// 如果用户名有变更,检查是否与其他用户重复
@ -89,7 +89,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (existingUser != null && existingUser.Id != id)
{
logger.LogWarning("更新管理员失败,用户名已存在: {UserName}", input.UserName);
throw new BusinessException("用户名已存在", 400);
throw new BusinessException("用户名已存在", ResultCode.CONFLICT);
}
adminUser.UserName = input.UserName.Trim();
@ -111,7 +111,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (!result)
{
logger.LogError("管理员更新失败ID: {Id}", id);
throw new BusinessException("更新管理员失败", 500);
throw new BusinessException("更新管理员失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("管理员更新成功ID: {Id}", id);
@ -130,14 +130,14 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (adminUser == null)
{
logger.LogWarning("未找到要删除的管理员ID: {Id}", id);
throw new BusinessException("管理员不存在", 404);
throw new BusinessException("管理员不存在", ResultCode.NOT_FOUND);
}
var result = await adminUserRepository.DeleteByIdAsync(id);
if (!result)
{
logger.LogError("管理员删除失败ID: {Id}", id);
throw new BusinessException("删除管理员失败", 500);
throw new BusinessException("删除管理员失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("管理员删除成功ID: {Id}", id);
@ -154,7 +154,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (adminUser == null)
{
logger.LogWarning("未找到管理员ID: {Id}", id);
throw new BusinessException("管理员不存在", 404);
throw new BusinessException("管理员不存在", ResultCode.NOT_FOUND);
}
return new AdminUserOutput
{
@ -178,12 +178,12 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
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;
var pageResult = await adminUserRepository.Queryable()
@ -215,7 +215,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (adminUser == null)
{
logger.LogWarning("未找到要更新状态的管理员ID: {Id}", id);
throw new BusinessException("管理员不存在", 404);
throw new BusinessException("管理员不存在", ResultCode.NOT_FOUND);
}
adminUser.Status = adminUser.Status==(int)DefaultStatusEnum.Active?(int)DefaultStatusEnum.Inactive:(int)DefaultStatusEnum.Active;
@ -226,7 +226,7 @@ public class AdminUserService(BaseRepository<AdminUser> adminUserRepository, ILo
if (!result)
{
logger.LogError("管理员状态更新失败ID: {Id}", id);
throw new BusinessException("更新管理员状态失败", 500);
throw new BusinessException("更新管理员状态失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("管理员状态更新成功ID: {Id}", id);