Refactor error handling in services to use internationalized error messages
- Updated OSSStorage validation errors to use internationalized messages. - Changed error messages in provider.go for unsupported file storage types. - Refactored tag_service.go to replace hardcoded error messages with internationalized versions. - Updated ticket_service.go to use internationalized error messages for various validation checks. - Refactored ticket_tag_service.go to use internationalized error messages for tag validation. - Changed ticket_view_service.go to use internationalized error messages for view validation. - Updated tool_catalog_service.go to use internationalized error messages for tool code validation. - Refactored user_service.go to replace error messages with internationalized versions. - Updated ws_service.go to use internationalized error messages for WebSocket handling. - Refactored wxwork_kf_inbound_service.go to use internationalized error messages for message handling. - Updated wxwork_kf_outbound_service.go to use internationalized error messages for outbound message handling. - Refactored wxwork_login_service.go to use internationalized error messages for login handling. - Updated login.go in wxwork package to use internationalized error messages for login state and ticket validation.
This commit is contained in:
@@ -63,111 +63,63 @@ func TestResolveLocalePrefersXLocale(t *testing.T) {
|
||||
func TestTranslateFallsBackToEnglish(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if got := TLocale(LocaleEnUS, "error.auth.expired", nil); got != "Your session has expired. Please sign in again." {
|
||||
if got := TLocale(LocaleEnUS, "error.auth.expired"); got != "Your session has expired. Please sign in again." {
|
||||
t.Fatalf("english translation = %q", got)
|
||||
}
|
||||
if got := TLocale("fr-FR", "error.auth.expired", nil); got != "Your session has expired. Please sign in again." {
|
||||
if got := TLocale("fr-FR", "error.auth.expired"); got != "Your session has expired. Please sign in again." {
|
||||
t.Fatalf("fallback translation = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateKnownMessageSupportsFormattedMessages(t *testing.T) {
|
||||
func TestGetfSupportsLocaleKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
message string
|
||||
want string
|
||||
name string
|
||||
locale string
|
||||
key string
|
||||
args []any
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "batch limit",
|
||||
message: "单次最多生成 31 条排班",
|
||||
want: "You can generate at most 31 schedule entries at a time.",
|
||||
name: "batch limit",
|
||||
locale: LocaleEnUS,
|
||||
key: "error.agentTeamSchedule.batchLimit",
|
||||
args: []any{31},
|
||||
want: "You can generate at most 31 schedule entries at a time.",
|
||||
},
|
||||
{
|
||||
name: "start date",
|
||||
message: "开始时间格式错误,请使用 yyyy-MM-dd",
|
||||
want: "Invalid start time format. Use yyyy-MM-dd.",
|
||||
name: "directory not found",
|
||||
locale: LocaleEnUS,
|
||||
key: "error.e0273",
|
||||
want: "Directory not found.",
|
||||
},
|
||||
{
|
||||
name: "end date time",
|
||||
message: "结束时间格式错误,请使用 yyyy-MM-dd HH:mm:ss 或 RFC3339",
|
||||
want: "Invalid end time format. Use yyyy-MM-dd HH:mm:ss or RFC3339.",
|
||||
name: "single knowledge base reference",
|
||||
locale: LocaleEnUS,
|
||||
key: "error.knowledgeBase.referencedByAgent",
|
||||
args: []any{"SalesBot"},
|
||||
want: "This knowledge base is referenced by AI Agent \"SalesBot\". Remove the binding first.",
|
||||
},
|
||||
{
|
||||
name: "oidc issuer config",
|
||||
message: "OIDC issuer 未配置",
|
||||
want: "OIDC issuer is not configured.",
|
||||
name: "multiple knowledge base references",
|
||||
locale: LocaleEnUS,
|
||||
key: "error.knowledgeBase.referencedByAgents",
|
||||
args: []any{3},
|
||||
want: "This knowledge base is referenced by 3 AI Agents. Remove the bindings first.",
|
||||
},
|
||||
{
|
||||
name: "oidc login result",
|
||||
message: "登录结果不能为空",
|
||||
want: "Sign-in result is required.",
|
||||
name: "faq import duplicated question",
|
||||
locale: LocaleEnUS,
|
||||
key: "error.knowledgeFAQImport.duplicateQuestionInFile",
|
||||
args: []any{12},
|
||||
want: "The standard question is duplicated in the same file. It first appeared on row 12.",
|
||||
},
|
||||
{
|
||||
name: "directory not found",
|
||||
message: "目录不存在",
|
||||
want: "Directory not found.",
|
||||
},
|
||||
{
|
||||
name: "directory has docs",
|
||||
message: "该目录下存在文档,无法删除",
|
||||
want: "This directory contains documents and cannot be deleted.",
|
||||
},
|
||||
{
|
||||
name: "move documents",
|
||||
message: "请选择要移动的文档",
|
||||
want: "Select documents to move.",
|
||||
},
|
||||
{
|
||||
name: "move faqs across knowledge bases",
|
||||
message: "只能移动当前知识库下的FAQ",
|
||||
want: "Only FAQs in the current knowledge base can be moved.",
|
||||
},
|
||||
{
|
||||
name: "single knowledge base reference",
|
||||
message: "知识库已被 AI Agent「SalesBot」引用,请先解除绑定",
|
||||
want: "This knowledge base is referenced by AI Agent \"SalesBot\". Remove the binding first.",
|
||||
},
|
||||
{
|
||||
name: "multiple knowledge base references",
|
||||
message: "知识库已被 3 个 AI Agent 引用,请先解除绑定",
|
||||
want: "This knowledge base is referenced by 3 AI Agents. Remove the bindings first.",
|
||||
},
|
||||
{
|
||||
name: "schedule conflict range",
|
||||
message: "该客服组在 2026-06-02 09:00:00 至 2026-06-02 10:00:00 已存在排班",
|
||||
want: "This agent team already has a schedule from 2026-06-02 09:00:00 to 2026-06-02 10:00:00.",
|
||||
},
|
||||
{
|
||||
name: "faq import duplicated question",
|
||||
message: "同一文件中标准问题重复,首次出现于第12行",
|
||||
want: "The standard question is duplicated in the same file. It first appeared on row 12.",
|
||||
},
|
||||
{
|
||||
name: "faq import existing question",
|
||||
message: "标准问题已存在,已跳过",
|
||||
want: "The standard question already exists and was skipped.",
|
||||
},
|
||||
{
|
||||
name: "required param",
|
||||
message: "参数:name不能为空",
|
||||
want: "Parameter \"name\" is required.",
|
||||
},
|
||||
{
|
||||
name: "mcp call failed",
|
||||
message: "调用 MCP 工具失败: timeout",
|
||||
want: "Failed to call MCP tool: timeout",
|
||||
},
|
||||
{
|
||||
name: "wxwork unsupported outbound",
|
||||
message: "当前暂不支持企业微信下行消息类型: voice",
|
||||
want: "The current WeCom outbound message type is not supported yet: voice",
|
||||
},
|
||||
{
|
||||
name: "wxwork callback missing",
|
||||
message: "企业微信登录回调地址未配置",
|
||||
want: "WeCom sign-in callback URL is not configured.",
|
||||
name: "zh keeps chinese value",
|
||||
locale: LocaleZhCN,
|
||||
key: "error.e0273",
|
||||
want: "目录不存在",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -175,19 +127,18 @@ func TestTranslateKnownMessageSupportsFormattedMessages(t *testing.T) {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := TranslateKnownMessage(LocaleEnUS, tt.message); got != tt.want {
|
||||
t.Fatalf("TranslateKnownMessage() = %q, want %q", got, tt.want)
|
||||
if got := Getf(tt.locale, tt.key, tt.args...); got != tt.want {
|
||||
t.Fatalf("Getf() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateKnownMessageKeepsChineseLocale(t *testing.T) {
|
||||
func TestGetfFallsBackToKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
message := "目录不存在"
|
||||
if got := TranslateKnownMessage(LocaleZhCN, message); got != message {
|
||||
t.Fatalf("TranslateKnownMessage() = %q, want %q", got, message)
|
||||
if got := Getf(LocaleEnUS, "missing.key"); got != "missing.key" {
|
||||
t.Fatalf("missing translation = %q, want %q", got, "missing.key")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user