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:
@ -83,7 +83,7 @@ public class AiChatService(
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input.Message))
|
||||
{
|
||||
throw new BusinessException("消息内容不能为空", 400);
|
||||
throw new BusinessException("消息内容不能为空", ResultCode.BAD_REQUEST);
|
||||
}
|
||||
|
||||
var apiKey = configuration["AiChat:ApiKey"];
|
||||
@ -95,7 +95,7 @@ public class AiChatService(
|
||||
|
||||
if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(baseUrl) || string.IsNullOrWhiteSpace(model))
|
||||
{
|
||||
throw new BusinessException("AI聊天服务配置不完整,请检查 AiChat 配置节", 500);
|
||||
throw new BusinessException("AI聊天服务配置不完整,请检查 AiChat 配置节", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
logger.LogInformation("开始调用AI聊天服务(流式),消息长度:{Length}", input.Message.Length);
|
||||
@ -132,7 +132,7 @@ public class AiChatService(
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
logger.LogError("AI聊天服务调用失败,状态码:{StatusCode},响应:{Response}", response.StatusCode, errorContent);
|
||||
throw new BusinessException($"AI服务调用失败:{response.StatusCode}", 500);
|
||||
throw new BusinessException($"AI服务调用失败:{response.StatusCode}", ResultCode.GLOBAL_ERROR);
|
||||
}
|
||||
|
||||
// 流式读取响应体
|
||||
|
||||
Reference in New Issue
Block a user