fix: 完善AI网关与Anthropic异常处理日志信息

- 图片生成解析失败时补充错误日志,便于问题定位
- Anthropic 非流式对话异常时,根据提示词过长场景补充返回信息
- 统一并优化 Anthropic 流式与非流式异常日志格式,提升可读性
This commit is contained in:
ccnetcore
2026-01-08 22:09:42 +08:00
parent c727aeed99
commit f1e8b66689
2 changed files with 15 additions and 5 deletions

View File

@@ -75,9 +75,16 @@ public class AnthropicChatCompletionsService(
{
Guid errorId = Guid.NewGuid();
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
logger.LogError($"Anthropic非流式对话异常 请求地址:{options.Endpoint},ErrorId{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}");
throw new Exception( $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】");
var message = $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】";
if (error.Contains("prompt is too long"))
{
message += $", Response: {error}";
}
logger.LogError(
$"Anthropic非流式对话异常 请求地址:{options.Endpoint},ErrorId{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}");
throw new Exception(message);
}
var value =
@@ -122,9 +129,11 @@ public class AnthropicChatCompletionsService(
{
Guid errorId = Guid.NewGuid();
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
logger.LogError($"Anthropic流式对话异常 请求地址:{options.Endpoint},ErrorId{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}");
logger.LogError(
$"Anthropic流式对话异常 请求地址:{options.Endpoint},ErrorId{errorId}, StatusCode: {response.StatusCode.GetHashCode()}, Response: {error}");
throw new Exception( $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】");
throw new Exception(
$"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】");
}
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));

View File

@@ -1080,6 +1080,7 @@ public class AiGateWayManager : DomainService
var imagePrefixBase64 = GeminiGenerateContentAcquirer.GetImagePrefixBase64(data);
if (string.IsNullOrWhiteSpace(imagePrefixBase64))
{
_logger.LogError($"图片生成解析失败模型id:,请求信息:【{request}】,请求响应信息:{imagePrefixBase64}");
throw new UserFriendlyException("大模型没有返回图片,请调整提示词或稍后再试");
}