diff --git a/pkg/xerr/errMsg.go b/pkg/xerr/errMsg.go index 82e7293..5c32531 100644 --- a/pkg/xerr/errMsg.go +++ b/pkg/xerr/errMsg.go @@ -37,6 +37,7 @@ func init() { TelegramNotBound: "Telegram not bound ", UserNotBindOauth: "User not bind oauth method", InviteCodeError: "Invite code error", + UserCommissionNotEnough: "佣金余额不足", RegisterIPLimit: "Too many registrations", EmailBindError: "Email already bound", UserBindInviteCodeExist: "Invite code already bound", @@ -82,6 +83,8 @@ func init() { // System error DebugModeError: "Debug mode is enabled", + SendSmsError: "短信发送失败", + GetAuthenticatorError: "Unsupported login method", AuthenticatorNotSupportedError: "The authenticator does not support this method", @@ -94,15 +97,18 @@ func init() { TelephoneExist: "Telephone already exists", DeviceExist: "device exists", PasswordIsEmpty: "password is empty", + AreaCodeIsEmpty: "国家区号不能为空", TelephoneError: "telephone number error", DeviceNotExist: "Device does not exist", UseridNotMatch: "Userid not match", + DeviceBindLimitExceeded: "设备绑定数量已达上限", // Order error OrderNotExist: "Order does not exist", PaymentMethodNotFound: "Payment method not found", OrderStatusError: "Order status error", InsufficientOfPeriod: "Insufficient number of period", + ExistAvailableTraffic: "存在可用流量", OrderAlreadyRefunded: "Order already refunded", OrderRefundNoSubscription: "Refund target subscription not found", OrderRefundCommissionMismatch: "Refund commission source not found", diff --git a/pkg/xerr/errMsg_test.go b/pkg/xerr/errMsg_test.go new file mode 100644 index 0000000..e244a52 --- /dev/null +++ b/pkg/xerr/errMsg_test.go @@ -0,0 +1,45 @@ +package xerr + +import "testing" + +// TestMapErrMsg_MissingCodesAreFilled 覆盖 HIF-138 中补齐的 5 个错误码: +// 这些码之前在 message 表中缺失,导致 MapErrMsg 回退到 "Internal Server Error"。 +func TestMapErrMsg_MissingCodesAreFilled(t *testing.T) { + cases := []struct { + name string + code uint32 + want string + }{ + {"UserCommissionNotEnough", UserCommissionNotEnough, "佣金余额不足"}, + {"SendSmsError", SendSmsError, "短信发送失败"}, + {"AreaCodeIsEmpty", AreaCodeIsEmpty, "国家区号不能为空"}, + {"DeviceBindLimitExceeded", DeviceBindLimitExceeded, "设备绑定数量已达上限"}, + {"ExistAvailableTraffic", ExistAvailableTraffic, "存在可用流量"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := MapErrMsg(tc.code) + if got == "Internal Server Error" { + t.Fatalf("MapErrMsg(%d) fell back to Internal Server Error; expected %q", tc.code, tc.want) + } + if got != tc.want { + t.Errorf("MapErrMsg(%d) = %q, want %q", tc.code, got, tc.want) + } + if !IsCodeErr(tc.code) { + t.Errorf("IsCodeErr(%d) = false, want true", tc.code) + } + }) + } +} + +// TestMapErrMsg_UnknownCodeFallsBack 验证未定义码仍然安全回退,不 panic。 +func TestMapErrMsg_UnknownCodeFallsBack(t *testing.T) { + const unknown uint32 = 99999 + if got := MapErrMsg(unknown); got != "Internal Server Error" { + t.Errorf("MapErrMsg(%d) = %q, want %q", unknown, got, "Internal Server Error") + } + if IsCodeErr(unknown) { + t.Errorf("IsCodeErr(%d) = true, want false", unknown) + } +}