Skip to content

添加渠道错误信息覆盖功能 - #5082

Closed
xin-zheqi wants to merge 1 commit into
QuantumNous:mainfrom
xin-zheqi:main
Closed

添加渠道错误信息覆盖功能#5082
xin-zheqi wants to merge 1 commit into
QuantumNous:mainfrom
xin-zheqi:main

Conversation

@xin-zheqi

@xin-zheqi xin-zheqi commented May 24, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

在渠道设置中加入了报错信息复写功能,能够覆盖上游返回的报错信息。
在日志中分别显示报错信息和错误码替换前后的内容。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

3 4

Summary by CodeRabbit

  • New Features

    • Added configurable error message mapping per channel to override upstream error codes and messages
    • Error override details now visible in usage logs, showing original vs. final status codes and error messages
  • Documentation

    • Added multilingual UI translations (English, French, Japanese, Russian, Vietnamese, Chinese) for error mapping configuration

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 88e08c4a-5c7b-4252-92c0-64d9ba0a77c1

📥 Commits

Reviewing files that changed from the base of the PR and between 0354c38 and d2b39ef.

📒 Files selected for processing (43)
  • constant/context_key.go
  • controller/relay.go
  • docker-compose.yml
  • middleware/distributor.go
  • model/channel.go
  • relay/audio_handler.go
  • relay/channel/gemini/relay-gemini.go
  • relay/chat_completions_via_responses.go
  • relay/claude_handler.go
  • relay/compatible_handler.go
  • relay/embedding_handler.go
  • relay/gemini_handler.go
  • relay/image_handler.go
  • relay/rerank_handler.go
  • relay/responses_handler.go
  • relay/websocket.go
  • service/error.go
  • service/error_test.go
  • types/error.go
  • web/classic/src/components/table/channels/modals/EditChannelModal.jsx
  • web/classic/src/hooks/usage-logs/useUsageLogsData.jsx
  • web/classic/src/i18n/locales/en.json
  • web/classic/src/i18n/locales/fr.json
  • web/classic/src/i18n/locales/ja.json
  • web/classic/src/i18n/locales/ru.json
  • web/classic/src/i18n/locales/vi.json
  • web/classic/src/i18n/locales/zh-CN.json
  • web/classic/src/i18n/locales/zh-TW.json
  • web/classic/src/i18n/locales/zh.json
  • web/default/src/features/channels/components/drawers/channel-mutate-drawer.tsx
  • web/default/src/features/channels/constants.ts
  • web/default/src/features/channels/lib/channel-form.ts
  • web/default/src/features/channels/types.ts
  • web/default/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • web/default/src/features/usage-logs/types.ts
  • web/default/src/i18n/locales/_reports/fr.untranslated.json
  • web/default/src/i18n/locales/_reports/ja.untranslated.json
  • web/default/src/i18n/locales/en.json
  • web/default/src/i18n/locales/fr.json
  • web/default/src/i18n/locales/ja.json
  • web/default/src/i18n/locales/ru.json
  • web/default/src/i18n/locales/vi.json
  • web/default/src/i18n/locales/zh.json

Walkthrough

This pull request introduces configurable per-channel error message mapping, enabling operators to override upstream error messages and status codes based on the original status code returned. The feature spans backend service logic, relay handler integration, middleware wiring, and frontend configuration UI with localized labels across eight language locales. A Docker Compose configuration change enables local development.

Changes

Error Override Mechanism

Layer / File(s) Summary
Channel Configuration & Context Keys
constant/context_key.go, model/channel.go
Channel struct adds optional ErrorMessageMapping field with getter; two new ContextKey constants (ContextKeyChannelErrorMessageMapping, ContextKeyChannelErrorOverrideSummary) enable passing mapping and override summary through Gin request context.
Error Override Service Implementation
service/error.go, service/error_test.go
ApplyChannelErrorOverrides combines status-code and error-message mappings, populates summary struct with original/final values and rewrite flags; ApplyChannelTaskErrorMessageOverride updates task error messages; helper functions parse JSON mapping strings and lookup message overrides; two test cases cover full override and fallback-to-rewritten-status-code scenarios.
Error Type Serialization
types/error.go
NewAPIError.SetMessage now propagates message changes into the underlying RelayError payload (OpenAIError or ClaudeError) to ensure rewrites are reflected in serialized responses.
Relay Handler Error Override Wiring
relay/audio_handler.go, relay/chat_completions_via_responses.go, relay/claude_handler.go, relay/compatible_handler.go, relay/embedding_handler.go, relay/gemini_handler.go, relay/image_handler.go, relay/rerank_handler.go, relay/responses_handler.go, relay/websocket.go, relay/channel/gemini/relay-gemini.go
All relay handlers now read error_message_mapping from context and apply it alongside status-code mapping using ApplyChannelErrorOverrides, storing the override summary in context instead of calling ResetStatusCode.
Middleware & Controller Integration
middleware/distributor.go, controller/relay.go
Middleware stores error message mapping into request context during channel setup; controller logs override summary (original/final status codes and messages, rewrite flags) in error log metadata and applies task-level message override before returning task error responses.
Classic Frontend UI & Localization
web/classic/src/components/table/channels/modals/EditChannelModal.jsx, web/classic/src/hooks/usage-logs/useUsageLogsData.jsx, web/classic/src/i18n/locales/{en,fr,ja,ru,vi,zh-CN,zh-TW,zh}.json
Adds JSON editor for error-message-mapping in channel edit modal with template support, auto-expands advanced settings when mapping present, displays original/final override details in usage logs for admin users; localized UI labels across eight languages.
Default Frontend UI & Localization
web/default/src/features/channels/{components/drawers/channel-mutate-drawer.tsx,constants.ts,lib/channel-form.ts,types.ts}, web/default/src/features/usage-logs/{components/dialogs/details-dialog.tsx,types.ts}, web/default/src/i18n/locales/{en,fr,ja,ru,vi,zh}.json, web/default/src/i18n/locales/_reports/{fr,ja}.untranslated.json
Extends channel configuration form schema to include optional error-message-mapping field with placeholder/description constants, updates payload transformation functions, renders JSON editor in mutate drawer, conditionally displays error override details in usage log dialog for admin users; adds localized labels and Pancake payment integration strings across locale files.
Local Build Configuration
docker-compose.yml
Changes new-api service from pulling calciumion/new-api:latest to building locally from repository root (build.context: ., build.dockerfile: Dockerfile) with new-api:latest tag.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • QuantumNous/new-api#2629: Introduces the chatCompletionsViaResponses relay flow that this PR extends with error-message-mapping support.

Suggested reviewers

  • Calcium-Ion
  • seefs001

Poem

🐰 With whiskers twitching, I review the code,
Error messages now twist and bend,
Status codes transform along the road,
Mapping dreams that flow and transcend!
Admin eyes see each clever recode,
A symphony where bugs now end! 🎵

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@seefs001

Copy link
Copy Markdown
Collaborator

暂不考虑

@seefs001 seefs001 closed this May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants