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:
@ -27,12 +27,12 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
{
|
||||
throw new BusinessException("勋章名称不能为空", 400);
|
||||
throw new BusinessException("勋章名称不能为空", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Type))
|
||||
{
|
||||
throw new BusinessException("勋章类型不能为空", 400);
|
||||
throw new BusinessException("勋章类型不能为空", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var medal = new Medal
|
||||
@ -58,7 +58,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
var result = await medalRepository.InsertAsync(medal);
|
||||
if (!result)
|
||||
{
|
||||
throw new BusinessException("创建勋章失败", 500);
|
||||
throw new BusinessException("创建勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
if (input.Rules != null && input.Rules.Count > 0)
|
||||
@ -74,7 +74,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "勋章创建事务失败,勋章名称: {Name}", input.Name);
|
||||
throw new BusinessException("创建勋章失败", 500);
|
||||
throw new BusinessException("创建勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("勋章创建成功,勋章名称: {Name}, ID: {Id}", input.Name, medal.Id);
|
||||
@ -94,17 +94,17 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (medal == null)
|
||||
{
|
||||
logger.LogWarning("未找到要更新的勋章,ID: {Id}", id);
|
||||
throw new BusinessException("勋章不存在", 404);
|
||||
throw new BusinessException("勋章不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Name))
|
||||
{
|
||||
throw new BusinessException("勋章名称不能为空", 400);
|
||||
throw new BusinessException("勋章名称不能为空", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input.Type))
|
||||
{
|
||||
throw new BusinessException("勋章类型不能为空", 400);
|
||||
throw new BusinessException("勋章类型不能为空", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
medal.Name = input.Name.Trim();
|
||||
@ -123,7 +123,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
var result = await medalRepository.UpdateAsync(medal);
|
||||
if (!result)
|
||||
{
|
||||
throw new BusinessException("更新勋章失败", 500);
|
||||
throw new BusinessException("更新勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
// 删除旧规则,重新插入新规则
|
||||
@ -145,7 +145,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "勋章更新事务失败,ID: {Id}", id);
|
||||
throw new BusinessException("更新勋章失败", 500);
|
||||
throw new BusinessException("更新勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("勋章更新成功,ID: {Id}", id);
|
||||
@ -165,7 +165,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (medal == null)
|
||||
{
|
||||
logger.LogWarning("未找到要删除的勋章,ID: {Id}", id);
|
||||
throw new BusinessException("勋章不存在", 404);
|
||||
throw new BusinessException("勋章不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
try
|
||||
@ -175,7 +175,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
var result = await medalRepository.DeleteByIdAsync(id);
|
||||
if (!result)
|
||||
{
|
||||
throw new BusinessException("删除勋章失败", 500);
|
||||
throw new BusinessException("删除勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
// 同时软删除关联规则
|
||||
@ -192,7 +192,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "勋章删除事务失败,ID: {Id}", id);
|
||||
throw new BusinessException("删除勋章失败", 500);
|
||||
throw new BusinessException("删除勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("勋章删除成功,ID: {Id}", id);
|
||||
@ -209,7 +209,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (medal == null)
|
||||
{
|
||||
logger.LogWarning("未找到勋章,ID: {Id}", id);
|
||||
throw new BusinessException("勋章不存在", 404);
|
||||
throw new BusinessException("勋章不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
var rules = await GetRulesByMedalIdAsync(medal.Id);
|
||||
@ -225,12 +225,12 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
|
||||
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;
|
||||
@ -266,7 +266,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (medal == null)
|
||||
{
|
||||
logger.LogWarning("未找到要更新状态的勋章,ID: {Id}", id);
|
||||
throw new BusinessException("勋章不存在", 404);
|
||||
throw new BusinessException("勋章不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
medal.Status = medal.Status == 0 ? 1 : 0;
|
||||
@ -277,7 +277,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("勋章状态更新失败,ID: {Id}", id);
|
||||
throw new BusinessException("更新勋章状态失败", 500);
|
||||
throw new BusinessException("更新勋章状态失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
logger.LogInformation("勋章状态更新成功,ID: {Id}, Status: {Status}", id, medal.Status);
|
||||
return result;
|
||||
@ -364,19 +364,19 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
|
||||
if (input.MedalId <= 0)
|
||||
{
|
||||
throw new BusinessException("勋章ID无效", 400);
|
||||
throw new BusinessException("勋章ID无效", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var medal = await medalRepository.GetByIdAsync(input.MedalId);
|
||||
if (medal == null)
|
||||
{
|
||||
logger.LogWarning("未找到要激活的勋章,勋章ID: {MedalId}", input.MedalId);
|
||||
throw new BusinessException("勋章不存在", 404);
|
||||
throw new BusinessException("勋章不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (medal.Status != 1)
|
||||
{
|
||||
throw new BusinessException("该勋章当前不可获得", 400);
|
||||
throw new BusinessException("该勋章当前不可获得", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
// 规则校验
|
||||
@ -384,7 +384,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (!passed)
|
||||
{
|
||||
logger.LogWarning("勋章规则校验未通过,用户ID: {UserId}, 勋章ID: {MedalId}, 原因: {Reason}", userId, input.MedalId, failReason);
|
||||
throw new BusinessException(failReason!, 400);
|
||||
throw new BusinessException(failReason!, ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var existingUserMedal = await Context.Queryable<UserMedal>()
|
||||
@ -393,7 +393,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
|
||||
if (existingUserMedal != null)
|
||||
{
|
||||
throw new BusinessException("您已拥有该勋章", 400);
|
||||
throw new BusinessException("您已拥有该勋章", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var userMedal = new UserMedal
|
||||
@ -414,7 +414,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (insertResult <= 0)
|
||||
{
|
||||
logger.LogError("勋章激活失败,用户ID: {UserId}, 勋章ID: {MedalId}", userId, input.MedalId);
|
||||
throw new BusinessException("激活勋章失败", 500);
|
||||
throw new BusinessException("激活勋章失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("勋章激活成功,用户ID: {UserId}, 勋章ID: {MedalId}", userId, input.MedalId);
|
||||
@ -486,7 +486,7 @@ public class MedalService(BaseRepository<Medal> medalRepository, IOptions<MedalR
|
||||
if (count <= 0)
|
||||
{
|
||||
logger.LogError("勋章规则插入失败,MedalId: {MedalId}", medalId);
|
||||
throw new BusinessException("创建勋章规则失败", 500);
|
||||
throw new BusinessException("创建勋章规则失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("勋章规则创建成功,MedalId: {MedalId}, 规则数: {Count}", medalId, rules.Count);
|
||||
|
||||
Reference in New Issue
Block a user