Files
xinghuoapi/backend/ent/errorpassthroughrule.go
shaw 39e05a2dad feat: 新增全局错误透传规则功能
支持管理员配置上游错误如何返回给客户端:
- 新增 ErrorPassthroughRule 数据模型和 Ent Schema
- 实现规则的 CRUD API(/admin/error-passthrough-rules)
- 支持按错误码、关键词匹配,支持 any/all 匹配模式
- 支持按平台过滤(anthropic/openai/gemini/antigravity)
- 支持透传或自定义响应状态码和错误消息
- 实现两级缓存(Redis + 本地内存)和多实例同步
- 集成到 gateway_handler 的错误处理流程
- 新增前端管理界面组件
- 新增单元测试覆盖核心匹配逻辑

优化:
- 移除 refreshLocalCache 中的冗余排序(数据库已排序)
- 后端 Validate() 增加匹配条件非空校验
2026-02-05 21:52:54 +08:00

270 lines
10 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"encoding/json"
"fmt"
"strings"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/Wei-Shaw/sub2api/ent/errorpassthroughrule"
)
// ErrorPassthroughRule is the model entity for the ErrorPassthroughRule schema.
type ErrorPassthroughRule struct {
config `json:"-"`
// ID of the ent.
ID int64 `json:"id,omitempty"`
// CreatedAt holds the value of the "created_at" field.
CreatedAt time.Time `json:"created_at,omitempty"`
// UpdatedAt holds the value of the "updated_at" field.
UpdatedAt time.Time `json:"updated_at,omitempty"`
// Name holds the value of the "name" field.
Name string `json:"name,omitempty"`
// Enabled holds the value of the "enabled" field.
Enabled bool `json:"enabled,omitempty"`
// Priority holds the value of the "priority" field.
Priority int `json:"priority,omitempty"`
// ErrorCodes holds the value of the "error_codes" field.
ErrorCodes []int `json:"error_codes,omitempty"`
// Keywords holds the value of the "keywords" field.
Keywords []string `json:"keywords,omitempty"`
// MatchMode holds the value of the "match_mode" field.
MatchMode string `json:"match_mode,omitempty"`
// Platforms holds the value of the "platforms" field.
Platforms []string `json:"platforms,omitempty"`
// PassthroughCode holds the value of the "passthrough_code" field.
PassthroughCode bool `json:"passthrough_code,omitempty"`
// ResponseCode holds the value of the "response_code" field.
ResponseCode *int `json:"response_code,omitempty"`
// PassthroughBody holds the value of the "passthrough_body" field.
PassthroughBody bool `json:"passthrough_body,omitempty"`
// CustomMessage holds the value of the "custom_message" field.
CustomMessage *string `json:"custom_message,omitempty"`
// Description holds the value of the "description" field.
Description *string `json:"description,omitempty"`
selectValues sql.SelectValues
}
// scanValues returns the types for scanning values from sql.Rows.
func (*ErrorPassthroughRule) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case errorpassthroughrule.FieldErrorCodes, errorpassthroughrule.FieldKeywords, errorpassthroughrule.FieldPlatforms:
values[i] = new([]byte)
case errorpassthroughrule.FieldEnabled, errorpassthroughrule.FieldPassthroughCode, errorpassthroughrule.FieldPassthroughBody:
values[i] = new(sql.NullBool)
case errorpassthroughrule.FieldID, errorpassthroughrule.FieldPriority, errorpassthroughrule.FieldResponseCode:
values[i] = new(sql.NullInt64)
case errorpassthroughrule.FieldName, errorpassthroughrule.FieldMatchMode, errorpassthroughrule.FieldCustomMessage, errorpassthroughrule.FieldDescription:
values[i] = new(sql.NullString)
case errorpassthroughrule.FieldCreatedAt, errorpassthroughrule.FieldUpdatedAt:
values[i] = new(sql.NullTime)
default:
values[i] = new(sql.UnknownType)
}
}
return values, nil
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the ErrorPassthroughRule fields.
func (_m *ErrorPassthroughRule) assignValues(columns []string, values []any) error {
if m, n := len(values), len(columns); m < n {
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
}
for i := range columns {
switch columns[i] {
case errorpassthroughrule.FieldID:
value, ok := values[i].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
_m.ID = int64(value.Int64)
case errorpassthroughrule.FieldCreatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field created_at", values[i])
} else if value.Valid {
_m.CreatedAt = value.Time
}
case errorpassthroughrule.FieldUpdatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field updated_at", values[i])
} else if value.Valid {
_m.UpdatedAt = value.Time
}
case errorpassthroughrule.FieldName:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field name", values[i])
} else if value.Valid {
_m.Name = value.String
}
case errorpassthroughrule.FieldEnabled:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field enabled", values[i])
} else if value.Valid {
_m.Enabled = value.Bool
}
case errorpassthroughrule.FieldPriority:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field priority", values[i])
} else if value.Valid {
_m.Priority = int(value.Int64)
}
case errorpassthroughrule.FieldErrorCodes:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field error_codes", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &_m.ErrorCodes); err != nil {
return fmt.Errorf("unmarshal field error_codes: %w", err)
}
}
case errorpassthroughrule.FieldKeywords:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field keywords", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &_m.Keywords); err != nil {
return fmt.Errorf("unmarshal field keywords: %w", err)
}
}
case errorpassthroughrule.FieldMatchMode:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field match_mode", values[i])
} else if value.Valid {
_m.MatchMode = value.String
}
case errorpassthroughrule.FieldPlatforms:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field platforms", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &_m.Platforms); err != nil {
return fmt.Errorf("unmarshal field platforms: %w", err)
}
}
case errorpassthroughrule.FieldPassthroughCode:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field passthrough_code", values[i])
} else if value.Valid {
_m.PassthroughCode = value.Bool
}
case errorpassthroughrule.FieldResponseCode:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field response_code", values[i])
} else if value.Valid {
_m.ResponseCode = new(int)
*_m.ResponseCode = int(value.Int64)
}
case errorpassthroughrule.FieldPassthroughBody:
if value, ok := values[i].(*sql.NullBool); !ok {
return fmt.Errorf("unexpected type %T for field passthrough_body", values[i])
} else if value.Valid {
_m.PassthroughBody = value.Bool
}
case errorpassthroughrule.FieldCustomMessage:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field custom_message", values[i])
} else if value.Valid {
_m.CustomMessage = new(string)
*_m.CustomMessage = value.String
}
case errorpassthroughrule.FieldDescription:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field description", values[i])
} else if value.Valid {
_m.Description = new(string)
*_m.Description = value.String
}
default:
_m.selectValues.Set(columns[i], values[i])
}
}
return nil
}
// Value returns the ent.Value that was dynamically selected and assigned to the ErrorPassthroughRule.
// This includes values selected through modifiers, order, etc.
func (_m *ErrorPassthroughRule) Value(name string) (ent.Value, error) {
return _m.selectValues.Get(name)
}
// Update returns a builder for updating this ErrorPassthroughRule.
// Note that you need to call ErrorPassthroughRule.Unwrap() before calling this method if this ErrorPassthroughRule
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *ErrorPassthroughRule) Update() *ErrorPassthroughRuleUpdateOne {
return NewErrorPassthroughRuleClient(_m.config).UpdateOne(_m)
}
// Unwrap unwraps the ErrorPassthroughRule entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *ErrorPassthroughRule) Unwrap() *ErrorPassthroughRule {
_tx, ok := _m.config.driver.(*txDriver)
if !ok {
panic("ent: ErrorPassthroughRule is not a transactional entity")
}
_m.config.driver = _tx.drv
return _m
}
// String implements the fmt.Stringer.
func (_m *ErrorPassthroughRule) String() string {
var builder strings.Builder
builder.WriteString("ErrorPassthroughRule(")
builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
builder.WriteString("created_at=")
builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("updated_at=")
builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
builder.WriteString(", ")
builder.WriteString("name=")
builder.WriteString(_m.Name)
builder.WriteString(", ")
builder.WriteString("enabled=")
builder.WriteString(fmt.Sprintf("%v", _m.Enabled))
builder.WriteString(", ")
builder.WriteString("priority=")
builder.WriteString(fmt.Sprintf("%v", _m.Priority))
builder.WriteString(", ")
builder.WriteString("error_codes=")
builder.WriteString(fmt.Sprintf("%v", _m.ErrorCodes))
builder.WriteString(", ")
builder.WriteString("keywords=")
builder.WriteString(fmt.Sprintf("%v", _m.Keywords))
builder.WriteString(", ")
builder.WriteString("match_mode=")
builder.WriteString(_m.MatchMode)
builder.WriteString(", ")
builder.WriteString("platforms=")
builder.WriteString(fmt.Sprintf("%v", _m.Platforms))
builder.WriteString(", ")
builder.WriteString("passthrough_code=")
builder.WriteString(fmt.Sprintf("%v", _m.PassthroughCode))
builder.WriteString(", ")
if v := _m.ResponseCode; v != nil {
builder.WriteString("response_code=")
builder.WriteString(fmt.Sprintf("%v", *v))
}
builder.WriteString(", ")
builder.WriteString("passthrough_body=")
builder.WriteString(fmt.Sprintf("%v", _m.PassthroughBody))
builder.WriteString(", ")
if v := _m.CustomMessage; v != nil {
builder.WriteString("custom_message=")
builder.WriteString(*v)
}
builder.WriteString(", ")
if v := _m.Description; v != nil {
builder.WriteString("description=")
builder.WriteString(*v)
}
builder.WriteByte(')')
return builder.String()
}
// ErrorPassthroughRules is a parsable slice of ErrorPassthroughRule.
type ErrorPassthroughRules []*ErrorPassthroughRule