fix: 为 Anthropic 聊天异常日志添加 ErrorId 并优化异常提示
在非流式与流式错误分支中生成 errorId,记录到日志并在抛出的异常中返回该 errorId,避免直接暴露完整响应内容并便于排查。调整了日志模板和异常提示文本。
This commit is contained in:
@@ -73,12 +73,14 @@ public class AnthropicChatCompletionsService(
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
Guid errorId = Guid.NewGuid();
|
||||
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,
|
||||
errorId,
|
||||
response.StatusCode, error);
|
||||
|
||||
throw new Exception( $"恭喜你运气爆棚遇到了错误,尊享包对话异常:StatusCode【{response.StatusCode}】,Response【{error}】");
|
||||
throw new Exception( $"恭喜你运气爆棚遇到了错误,尊享包对话异常:StatusCode【{response.StatusCode.GetHashCode()}】,ErrorId【{errorId}】");
|
||||
}
|
||||
|
||||
var value =
|
||||
@@ -121,12 +123,14 @@ public class AnthropicChatCompletionsService(
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
Guid errorId = Guid.NewGuid();
|
||||
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,
|
||||
errorId,
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user