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:
@ -28,7 +28,7 @@ public class PointsService(
|
||||
input.UserId, input.Amount, input.ChangeType);
|
||||
|
||||
if (input.Amount <= 0)
|
||||
throw new BusinessException("增加积分数量必须大于0", 400);
|
||||
throw new BusinessException("增加积分数量必须大于0", ResultCode.BAD_REQUEST);
|
||||
|
||||
AddPointsOutput result = null!;
|
||||
|
||||
@ -49,7 +49,7 @@ public class PointsService(
|
||||
input.UserId, input.Amount, input.ChangeType);
|
||||
|
||||
if (input.Amount <= 0)
|
||||
throw new BusinessException("扣除积分数量必须大于0", 400);
|
||||
throw new BusinessException("扣除积分数量必须大于0", ResultCode.BAD_REQUEST);
|
||||
|
||||
DeductPointsOutput result = null!;
|
||||
|
||||
@ -71,7 +71,7 @@ public class PointsService(
|
||||
public async Task<AddPointsOutput> AddPointsInTranAsync(AddPointsInput input)
|
||||
{
|
||||
if (input.Amount <= 0)
|
||||
throw new BusinessException("增加积分数量必须大于0", 400);
|
||||
throw new BusinessException("增加积分数量必须大于0", ResultCode.BAD_REQUEST);
|
||||
|
||||
// 查询用户当前积分
|
||||
var user = await Context.Queryable<Users>()
|
||||
@ -79,7 +79,7 @@ public class PointsService(
|
||||
.FirstAsync();
|
||||
|
||||
if (user == null)
|
||||
throw new BusinessException("用户不存在", 404);
|
||||
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
|
||||
|
||||
var previousBalance = user.Points;
|
||||
var newBalance = previousBalance + input.Amount;
|
||||
@ -129,7 +129,7 @@ public class PointsService(
|
||||
public async Task<DeductPointsOutput> DeductPointsInTranAsync(DeductPointsInput input)
|
||||
{
|
||||
if (input.Amount <= 0)
|
||||
throw new BusinessException("扣除积分数量必须大于0", 400);
|
||||
throw new BusinessException("扣除积分数量必须大于0", ResultCode.BAD_REQUEST);
|
||||
|
||||
// 查询用户当前积分
|
||||
var user = await Context.Queryable<Users>()
|
||||
@ -137,13 +137,13 @@ public class PointsService(
|
||||
.FirstAsync();
|
||||
|
||||
if (user == null)
|
||||
throw new BusinessException("用户不存在", 404);
|
||||
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
|
||||
|
||||
var previousBalance = user.Points;
|
||||
|
||||
// 余额不足校验
|
||||
if (previousBalance < input.Amount)
|
||||
throw new BusinessException($"积分不足,需要 {input.Amount} 积分,当前余额 {previousBalance}", 400);
|
||||
throw new BusinessException($"积分不足,需要 {input.Amount} 积分,当前余额 {previousBalance}", ResultCode.BAD_REQUEST);
|
||||
|
||||
var newBalance = previousBalance - input.Amount;
|
||||
|
||||
@ -214,7 +214,7 @@ public class PointsService(
|
||||
.FirstAsync();
|
||||
|
||||
if (user == null)
|
||||
throw new BusinessException("用户不存在", 404);
|
||||
throw new BusinessException("用户不存在", ResultCode.NOT_FOUND);
|
||||
|
||||
// 查询累计收入(Income 类型)
|
||||
var totalIncome = await Context.Queryable<PointsRecord>()
|
||||
@ -241,10 +241,10 @@ public class PointsService(
|
||||
public async Task<PageListModel<PointsRecordOutput>> GetPointsRecordsAsync(PointsRecordQueryInput input)
|
||||
{
|
||||
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);
|
||||
|
||||
var query = Context.Queryable<PointsRecord>()
|
||||
.Where(r => r.UserId == input.UserId && !r.IsDeleted)
|
||||
|
||||
Reference in New Issue
Block a user