error_passthrough_rule.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Package model 定义服务层使用的数据模型。
  2. package model
  3. import (
  4. "time"
  5. "github.com/Wei-Shaw/sub2api/internal/domain"
  6. )
  7. // ErrorPassthroughRule 全局错误透传规则
  8. // 用于控制上游错误如何返回给客户端
  9. type ErrorPassthroughRule struct {
  10. ID int64 `json:"id"`
  11. Name string `json:"name"` // 规则名称
  12. Enabled bool `json:"enabled"` // 是否启用
  13. Priority int `json:"priority"` // 优先级(数字越小优先级越高)
  14. ErrorCodes []int `json:"error_codes"` // 匹配的错误码列表(OR关系)
  15. Keywords []string `json:"keywords"` // 匹配的关键词列表(OR关系)
  16. MatchMode string `json:"match_mode"` // "any"(任一条件) 或 "all"(所有条件)
  17. Platforms []string `json:"platforms"` // 适用平台列表
  18. PassthroughCode bool `json:"passthrough_code"` // 是否透传原始状态码
  19. ResponseCode *int `json:"response_code"` // 自定义状态码(passthrough_code=false 时使用)
  20. PassthroughBody bool `json:"passthrough_body"` // 是否透传原始错误信息
  21. CustomMessage *string `json:"custom_message"` // 自定义错误信息(passthrough_body=false 时使用)
  22. SkipMonitoring bool `json:"skip_monitoring"` // 是否跳过运维监控记录
  23. Description *string `json:"description"` // 规则描述
  24. CreatedAt time.Time `json:"created_at"`
  25. UpdatedAt time.Time `json:"updated_at"`
  26. }
  27. // MatchModeAny 表示任一条件匹配即可
  28. const MatchModeAny = "any"
  29. // MatchModeAll 表示所有条件都必须匹配
  30. const MatchModeAll = "all"
  31. // 支持的平台常量
  32. const (
  33. PlatformAnthropic = domain.PlatformAnthropic
  34. PlatformOpenAI = domain.PlatformOpenAI
  35. PlatformGemini = domain.PlatformGemini
  36. PlatformAntigravity = domain.PlatformAntigravity
  37. PlatformGrok = domain.PlatformGrok
  38. PlatformKimi = domain.PlatformKimi
  39. PlatformZhipu = domain.PlatformZhipu
  40. PlatformDeepseek = domain.PlatformDeepseek
  41. PlatformMiniMax = domain.PlatformMiniMax
  42. )
  43. // AllPlatforms 返回所有支持的平台列表
  44. func AllPlatforms() []string {
  45. return []string{
  46. PlatformAnthropic,
  47. PlatformOpenAI,
  48. PlatformGemini,
  49. PlatformAntigravity,
  50. PlatformGrok,
  51. PlatformKimi,
  52. PlatformZhipu,
  53. PlatformDeepseek,
  54. PlatformMiniMax,
  55. }
  56. }
  57. // Validate 验证规则配置的有效性
  58. func (r *ErrorPassthroughRule) Validate() error {
  59. if r.Name == "" {
  60. return &ValidationError{Field: "name", Message: "name is required"}
  61. }
  62. if r.MatchMode != MatchModeAny && r.MatchMode != MatchModeAll {
  63. return &ValidationError{Field: "match_mode", Message: "match_mode must be 'any' or 'all'"}
  64. }
  65. // 至少需要配置一个匹配条件(错误码或关键词)
  66. if len(r.ErrorCodes) == 0 && len(r.Keywords) == 0 {
  67. return &ValidationError{Field: "conditions", Message: "at least one error_code or keyword is required"}
  68. }
  69. if !r.PassthroughCode && (r.ResponseCode == nil || *r.ResponseCode <= 0) {
  70. return &ValidationError{Field: "response_code", Message: "response_code is required when passthrough_code is false"}
  71. }
  72. if !r.PassthroughBody && (r.CustomMessage == nil || *r.CustomMessage == "") {
  73. return &ValidationError{Field: "custom_message", Message: "custom_message is required when passthrough_body is false"}
  74. }
  75. return nil
  76. }
  77. // ValidationError 表示验证错误
  78. type ValidationError struct {
  79. Field string
  80. Message string
  81. }
  82. func (e *ValidationError) Error() string {
  83. return e.Field + ": " + e.Message
  84. }