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:
@ -29,17 +29,17 @@ public class ProductService(
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (input.Price < 0)
|
||||
{
|
||||
throw new BusinessException("商品价格不能为负数", 400);
|
||||
throw new BusinessException("商品价格不能为负数", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var product = new Product
|
||||
@ -66,7 +66,7 @@ public class ProductService(
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品创建失败,商品名称: {Name}", input.Name);
|
||||
throw new BusinessException("创建商品失败", 500);
|
||||
throw new BusinessException("创建商品失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
// 将 temp 目录下的图片搬运到正式目录
|
||||
@ -113,22 +113,22 @@ public class ProductService(
|
||||
if (product == 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);
|
||||
}
|
||||
|
||||
if (input.Price < 0)
|
||||
{
|
||||
throw new BusinessException("商品价格不能为负数", 400);
|
||||
throw new BusinessException("商品价格不能为负数", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
// 将 temp 目录下的新图片搬运到正式目录
|
||||
@ -158,7 +158,7 @@ public class ProductService(
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品更新失败,ID: {Id}", id);
|
||||
throw new BusinessException("更新商品失败", 500);
|
||||
throw new BusinessException("更新商品失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("商品更新成功,ID: {Id}", id);
|
||||
@ -195,14 +195,14 @@ public class ProductService(
|
||||
if (product == null)
|
||||
{
|
||||
logger.LogWarning("未找到要删除的商品,ID: {Id}", id);
|
||||
throw new BusinessException("商品不存在", 404);
|
||||
throw new BusinessException("商品不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
var result = await productRepository.DeleteByIdAsync(id);
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品删除失败,ID: {Id}", id);
|
||||
throw new BusinessException("删除商品失败", 500);
|
||||
throw new BusinessException("删除商品失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("商品删除成功,ID: {Id}", id);
|
||||
@ -219,7 +219,7 @@ public class ProductService(
|
||||
if (product == null)
|
||||
{
|
||||
logger.LogWarning("未找到商品,ID: {Id}", id);
|
||||
throw new BusinessException("商品不存在", 404);
|
||||
throw new BusinessException("商品不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
return new ProductOutput
|
||||
@ -252,12 +252,12 @@ public class ProductService(
|
||||
|
||||
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;
|
||||
@ -303,7 +303,7 @@ public class ProductService(
|
||||
if (product == null)
|
||||
{
|
||||
logger.LogWarning("未找到要更新状态的商品,ID: {Id}", id);
|
||||
throw new BusinessException("商品不存在", 404);
|
||||
throw new BusinessException("商品不存在", ResultCode.NOT_FOUND);
|
||||
}
|
||||
|
||||
product.Status = product.Status == (int)ProductStatusEnum.OnSale
|
||||
@ -316,7 +316,7 @@ public class ProductService(
|
||||
if (!result)
|
||||
{
|
||||
logger.LogError("商品状态更新失败,ID: {Id}", id);
|
||||
throw new BusinessException("更新商品状态失败", 500);
|
||||
throw new BusinessException("更新商品状态失败", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("商品上下架状态更新成功,ID: {Id}, SaleStatus: {SaleStatus}", id, product.Status);
|
||||
|
||||
Reference in New Issue
Block a user