From c797a848472c1764fc5a76ab4c287b4724651c2b Mon Sep 17 00:00:00 2001 From: Kayvan Sylvan Date: Mon, 16 Feb 2026 08:38:35 -0800 Subject: [PATCH] feat: add error handling for fetch operations in i18n - Add new error messages for fetch operations - Rename `errInvalidLocationFormat` to `errInvalidLocationFormatKey` - Use `errors.New` instead of `fmt.Errorf` for fetch errors - Update Chinese locale with fetch-related error messages --- internal/i18n/locales/zh.json | 12 +++++++++++- internal/plugins/ai/gemini/gemini.go | 4 ++-- internal/plugins/template/fetch.go | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/i18n/locales/zh.json b/internal/i18n/locales/zh.json index 9c3cc548..3125d673 100644 --- a/internal/i18n/locales/zh.json +++ b/internal/i18n/locales/zh.json @@ -481,5 +481,15 @@ "bedrock_converse_failed": "模型 %s 的 bedrock converse 失败:%w", "bedrock_unexpected_response_type": "意外的响应类型:%T", "bedrock_empty_response_content": "空响应内容", - "bedrock_unexpected_content_block_type": "意外的内容块类型:%T" + "bedrock_unexpected_content_block_type": "意外的内容块类型:%T", + "fetch_unknown_operation": "fetch:未知操作 %q(支持:get)", + "fetch_content_not_utf8": "fetch:内容不是有效的 UTF-8 文本", + "fetch_content_null_bytes": "fetch:内容包含空字节", + "fetch_error_create_request": "fetch:创建请求时出错:%v", + "fetch_error_fetching_url": "fetch:获取 URL 时出错:%v", + "fetch_http_error": "fetch:HTTP 错误:%d - %s", + "fetch_content_too_large": "fetch:内容过大:%d 字节(最大 %d 字节)", + "fetch_unsupported_content_type": "fetch:不支持的内容类型 %q - 仅允许文本内容", + "fetch_error_reading_response": "fetch:读取响应时出错:%v", + "fetch_content_exceeds_limit": "fetch:内容过大:超过 %d 字节" } diff --git a/internal/plugins/ai/gemini/gemini.go b/internal/plugins/ai/gemini/gemini.go index 96625d6c..81a4d64c 100644 --- a/internal/plugins/ai/gemini/gemini.go +++ b/internal/plugins/ai/gemini/gemini.go @@ -31,7 +31,7 @@ const ( ) const ( - errInvalidLocationFormat = "gemini_invalid_location_format" + errInvalidLocationFormatKey = "gemini_invalid_location_format" locationSeparator = "/" langCodeSeparator = "_" langCodeNormalizedSep = "-" @@ -232,7 +232,7 @@ func (o *Client) buildGenerateContentConfig(opts *domain.ChatOptions) (*genai.Ge RetrievalConfig: &genai.RetrievalConfig{LanguageCode: loc}, } } else { - return nil, fmt.Errorf(i18n.T(errInvalidLocationFormat), loc) + return nil, fmt.Errorf(i18n.T(errInvalidLocationFormatKey), loc) } } } diff --git a/internal/plugins/template/fetch.go b/internal/plugins/template/fetch.go index 33ba3686..3c746fbc 100644 --- a/internal/plugins/template/fetch.go +++ b/internal/plugins/template/fetch.go @@ -72,11 +72,11 @@ func (p *FetchPlugin) validateTextContent(content []byte) error { debugf("Fetch: validating content length=%d bytes", len(content)) if !utf8.Valid(content) { - return fmt.Errorf(i18n.T("fetch_content_not_utf8")) + return errors.New(i18n.T("fetch_content_not_utf8")) } if bytes.Contains(content, []byte{0}) { - return fmt.Errorf(i18n.T("fetch_content_null_bytes")) + return errors.New(i18n.T("fetch_content_null_bytes")) } debugf("Fetch: content validation successful")