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,17 +27,17 @@ public class CheckInConfigService(
|
||||
|
||||
if (input.DayNumber <= 0)
|
||||
{
|
||||
throw new BusinessException("连续签到天数必须大于0", 400);
|
||||
throw new BusinessException("连续签到天数必须大于0", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (input.RewardPoints < 0)
|
||||
{
|
||||
throw new BusinessException("奖励积分不能为负数", 400);
|
||||
throw new BusinessException("奖励积分不能为负数", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (input.BonusPoints < 0)
|
||||
{
|
||||
throw new BusinessException("额外奖励积分不能为负数", 400);
|
||||
throw new BusinessException("额外奖励积分不能为负数", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
// 检查同类型下是否已存在相同天数配置
|
||||
@ -47,7 +47,7 @@ public class CheckInConfigService(
|
||||
|
||||
if (exists)
|
||||
{
|
||||
throw new BusinessException($"该类型下已存在连续{input.DayNumber}天的配置", 400);
|
||||
throw new BusinessException($"该类型下已存在连续{input.DayNumber}天的配置", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var config = new CheckInConfig
|
||||
@ -67,7 +67,7 @@ public class CheckInConfigService(
|
||||
var result = await checkInConfigRepository.InsertAsync(config);
|
||||
if (!result)
|
||||
{
|
||||
throw new BusinessException("创建签到配置失败", 500);
|
||||
throw new BusinessException("创建签到配置失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("签到配置创建成功,ID: {Id}", config.Id);
|
||||
@ -85,22 +85,22 @@ public class CheckInConfigService(
|
||||
if (config == null)
|
||||
{
|
||||
logger.LogWarning("未找到要更新的签到配置,ID: {Id}", id);
|
||||
throw new BusinessException("签到配置不存在", 404);
|
||||
throw new BusinessException("签到配置不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (input.DayNumber <= 0)
|
||||
{
|
||||
throw new BusinessException("连续签到天数必须大于0", 400);
|
||||
throw new BusinessException("连续签到天数必须大于0", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (input.RewardPoints < 0)
|
||||
{
|
||||
throw new BusinessException("奖励积分不能为负数", 400);
|
||||
throw new BusinessException("奖励积分不能为负数", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
if (input.BonusPoints < 0)
|
||||
{
|
||||
throw new BusinessException("额外奖励积分不能为负数", 400);
|
||||
throw new BusinessException("额外奖励积分不能为负数", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
// 检查同类型下是否已存在相同天数配置(排除自身)
|
||||
@ -110,7 +110,7 @@ public class CheckInConfigService(
|
||||
|
||||
if (exists)
|
||||
{
|
||||
throw new BusinessException($"该类型下已存在连续{input.DayNumber}天的配置", 400);
|
||||
throw new BusinessException($"该类型下已存在连续{input.DayNumber}天的配置", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
config.DayNumber = input.DayNumber;
|
||||
@ -123,7 +123,7 @@ public class CheckInConfigService(
|
||||
var updateResult = await checkInConfigRepository.UpdateAsync(config);
|
||||
if (!updateResult)
|
||||
{
|
||||
throw new BusinessException("更新签到配置失败", 500);
|
||||
throw new BusinessException("更新签到配置失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("签到配置更新成功,ID: {Id}", id);
|
||||
@ -141,7 +141,7 @@ public class CheckInConfigService(
|
||||
if (config == null)
|
||||
{
|
||||
logger.LogWarning("未找到要删除的签到配置,ID: {Id}", id);
|
||||
throw new BusinessException("签到配置不存在", 404);
|
||||
throw new BusinessException("签到配置不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
var result = await checkInConfigRepository.Context.Updateable<CheckInConfig>()
|
||||
@ -156,7 +156,7 @@ public class CheckInConfigService(
|
||||
|
||||
if (result <= 0)
|
||||
{
|
||||
throw new BusinessException("删除签到配置失败", 500);
|
||||
throw new BusinessException("删除签到配置失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("签到配置删除成功,ID: {Id}", id);
|
||||
@ -170,7 +170,7 @@ public class CheckInConfigService(
|
||||
var config = await checkInConfigRepository.GetByIdAsync(id);
|
||||
if (config == null)
|
||||
{
|
||||
throw new BusinessException("签到配置不存在", 404);
|
||||
throw new BusinessException("签到配置不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
return MapToOutput(config);
|
||||
@ -185,12 +185,12 @@ public class CheckInConfigService(
|
||||
|
||||
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;
|
||||
@ -217,7 +217,7 @@ public class CheckInConfigService(
|
||||
if (config == null)
|
||||
{
|
||||
logger.LogWarning("未找到要更新状态的签到配置,ID: {Id}", id);
|
||||
throw new BusinessException("签到配置不存在", 404);
|
||||
throw new BusinessException("签到配置不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
config.Status = config.Status == (int)DefaultStatusEnum.Active
|
||||
@ -230,7 +230,7 @@ public class CheckInConfigService(
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("签到配置状态更新失败,ID: {Id}", id);
|
||||
throw new BusinessException("更新签到配置状态失败", 500);
|
||||
throw new BusinessException("更新签到配置状态失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("签到配置状态更新成功,ID: {Id}, Status: {Status}", id, config.Status);
|
||||
|
||||
Reference in New Issue
Block a user