fix: 为 Anthropic 聊天异常日志添加 ErrorId 并优化异常提示

在非流式与流式错误分支中生成 errorId,记录到日志并在抛出的异常中返回该 errorId,避免直接暴露完整响应内容并便于排查。调整了日志模板和异常提示文本。
This commit is contained in:
chenchun
2026-01-07 18:02:38 +08:00
parent db7dc0e9a7
commit 55c17211d8

View File

@@ -73,12 +73,14 @@ public class AnthropicChatCompletionsService(
// 大于等于400的状态码都认为是异常 // 大于等于400的状态码都认为是异常
if (response.StatusCode >= HttpStatusCode.BadRequest) if (response.StatusCode >= HttpStatusCode.BadRequest)
{ {
Guid errorId = Guid.NewGuid();
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", logger.LogError("Anthropic非流式对话异常 请求地址:{Address},ErrorId{errorId}, StatusCode: {StatusCode.GetHashCode()} Response: {Response}",
options.Endpoint, options.Endpoint,
errorId,
response.StatusCode, error); response.StatusCode, error);
throw new Exception( $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode}】Response【{error}】"); throw new Exception( $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】");
} }
var value = var value =
@@ -121,12 +123,14 @@ public class AnthropicChatCompletionsService(
// 大于等于400的状态码都认为是异常 // 大于等于400的状态码都认为是异常
if (response.StatusCode >= HttpStatusCode.BadRequest) if (response.StatusCode >= HttpStatusCode.BadRequest)
{ {
Guid errorId = Guid.NewGuid();
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", logger.LogError("Anthropic流式对话异常 请求地址:{Address},ErrorId{errorId}, StatusCode: {StatusCode.GetHashCode()} Response: {Response}",
options.Endpoint, options.Endpoint,
errorId,
response.StatusCode, error); response.StatusCode, error);
throw new Exception("OpenAI对话异常" + response.StatusCode); throw new Exception( $"恭喜你运气爆棚遇到了错误尊享包对话异常StatusCode【{response.StatusCode.GetHashCode()}】ErrorId【{errorId}】");
} }
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken)); using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));