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

@ -24,8 +24,8 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
{
logger.LogInformation("正在查询社区消息列表,页码: {PageIndex}, 每页条数: {PageSize}", input.PageIndex, input.PageSize);
if (input.PageIndex <= 0) throw new BusinessException("页码必须大于0", 400);
if (input.PageSize <= 0 || input.PageSize > 100) throw new BusinessException("每页条数必须在1-100之间", 400);
if (input.PageIndex <= 0) throw new BusinessException("页码必须大于0", ResultCode.BAD_REQUEST);
if (input.PageSize <= 0 || input.PageSize > 100) throw new BusinessException("每页条数必须在1-100之间", ResultCode.BAD_REQUEST);
RefAsync<int> totalNumber = 0;
var pageResult = await messageRepository.Queryable()
@ -75,7 +75,7 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", 404);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
return new AdminMessageDetailOutput
@ -112,14 +112,14 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (message == null)
{
logger.LogWarning("未找到要删除的社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", 404);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
var result = await messageRepository.DeleteByIdAsync(id);
if (!result)
{
logger.LogError("社区消息删除失败ID: {Id}", id);
throw new BusinessException("删除消息失败", 500);
throw new BusinessException("删除消息失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息删除成功ID: {Id}", id);
@ -134,14 +134,14 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (status != 1 && status != 2)
{
throw new BusinessException("状态值无效只能为1(解冻/通过)或2(冻结)", 400);
throw new BusinessException("状态值无效只能为1(解冻/通过)或2(冻结)", ResultCode.BAD_REQUEST);
}
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", 404);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
message.Status = status;
@ -152,7 +152,7 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (!result)
{
logger.LogError("社区消息冻结状态更新失败ID: {Id}", id);
throw new BusinessException("更新冻结状态失败", 500);
throw new BusinessException("更新冻结状态失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息冻结状态更新成功ID: {Id}, Status: {Status}", id, status);
@ -167,14 +167,14 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (isFeatured != 0 && isFeatured != 1)
{
throw new BusinessException("精选值无效只能为0(取消)或1(精选)", 400);
throw new BusinessException("精选值无效只能为0(取消)或1(精选)", ResultCode.BAD_REQUEST);
}
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", 404);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
message.IsFeatured = isFeatured;
@ -185,7 +185,7 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (!result)
{
logger.LogError("社区消息精选设置失败ID: {Id}", id);
throw new BusinessException("设置精选失败", 500);
throw new BusinessException("设置精选失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息精选设置成功ID: {Id}, IsFeatured: {IsFeatured}", id, isFeatured);
@ -202,7 +202,7 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", 404);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
message.SortOrder = sortOrder;
@ -213,7 +213,7 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (!result)
{
logger.LogError("社区消息排序权重设置失败ID: {Id}", id);
throw new BusinessException("设置排序权重失败", 500);
throw new BusinessException("设置排序权重失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息排序权重设置成功ID: {Id}, SortOrder: {SortOrder}", id, sortOrder);
@ -228,7 +228,7 @@ public class CommunityMessageService(BaseRepository<CommunityMessage> messageRep
if (ids == null || ids.Count == 0)
{
throw new BusinessException("消息ID列表不能为空", 400);
throw new BusinessException("消息ID列表不能为空", ResultCode.BAD_REQUEST);
}
var result = await Context.Updateable<CommunityMessage>()