实现统一模型 ID 格式 (provider_id/model_name),支持跨协议模型标识和 Smart Passthrough。 核心变更: - 新增 pkg/modelid 包:解析、格式化、校验统一模型 ID - 数据库迁移:models 表使用 UUID 主键 + UNIQUE(provider_id, model_name) 约束 - Repository 层:FindByProviderAndModelName、ListEnabled 方法 - Service 层:联合唯一校验、provider ID 字符集校验 - Conversion 层:ExtractModelName、RewriteRequestModelName/RewriteResponseModelName 方法 - Handler 层:统一模型 ID 路由、Smart Passthrough、Models API 本地聚合 - 新增 error-responses、unified-model-id 规范 测试覆盖: - 单元测试:modelid、conversion、handler、service、repository - 集成测试:统一模型 ID 路由、Smart Passthrough 保真性、跨协议转换 - 迁移测试:UUID 主键、UNIQUE 约束、级联删除 OpenSpec: - 归档 unified-model-id 变更到 archive/2026-04-21-unified-model-id - 同步 11 个 delta specs 到 main specs - 新增 error-responses、unified-model-id 规范文件
92 lines
3.0 KiB
Go
92 lines
3.0 KiB
Go
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// AppError 结构化应用错误
|
|
type AppError struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
HTTPStatus int `json:"-"`
|
|
Cause error `json:"-"`
|
|
Context map[string]interface{} `json:"-"`
|
|
}
|
|
|
|
// Error implements error interface
|
|
func (e *AppError) Error() string {
|
|
if e.Cause != nil {
|
|
return fmt.Sprintf("%s: %s (%v)", e.Code, e.Message, e.Cause)
|
|
}
|
|
return fmt.Sprintf("%s: %s", e.Code, e.Message)
|
|
}
|
|
|
|
// Unwrap returns the underlying error
|
|
func (e *AppError) Unwrap() error {
|
|
return e.Cause
|
|
}
|
|
|
|
// WithCause returns a copy of the error with the given cause
|
|
func (e *AppError) WithCause(cause error) *AppError {
|
|
return &AppError{
|
|
Code: e.Code,
|
|
Message: e.Message,
|
|
HTTPStatus: e.HTTPStatus,
|
|
Cause: cause,
|
|
Context: e.Context,
|
|
}
|
|
}
|
|
|
|
// NewAppError creates a new AppError
|
|
func NewAppError(code, message string, httpStatus int) *AppError {
|
|
return &AppError{
|
|
Code: code,
|
|
Message: message,
|
|
HTTPStatus: httpStatus,
|
|
}
|
|
}
|
|
|
|
// Predefined errors
|
|
var (
|
|
ErrModelNotFound = NewAppError("model_not_found", "模型未找到", http.StatusNotFound)
|
|
ErrModelDisabled = NewAppError("model_disabled", "模型已禁用", http.StatusNotFound)
|
|
ErrProviderNotFound = NewAppError("provider_not_found", "供应商未找到", http.StatusNotFound)
|
|
ErrProviderDisabled = NewAppError("provider_disabled", "供应商已禁用", http.StatusNotFound)
|
|
ErrInvalidRequest = NewAppError("invalid_request", "无效的请求", http.StatusBadRequest)
|
|
ErrInternal = NewAppError("internal_error", "内部错误", http.StatusInternalServerError)
|
|
ErrDatabaseNotInit = NewAppError("database_not_initialized", "数据库未初始化", http.StatusInternalServerError)
|
|
ErrConflict = NewAppError("conflict", "资源已存在", http.StatusConflict)
|
|
ErrRequestCreate = NewAppError("request_create_error", "创建请求失败", http.StatusInternalServerError)
|
|
ErrRequestSend = NewAppError("request_send_error", "发送请求失败", http.StatusBadGateway)
|
|
ErrResponseRead = NewAppError("response_read_error", "读取响应失败", http.StatusBadGateway)
|
|
ErrInvalidProviderID = NewAppError("invalid_provider_id", "供应商 ID 仅允许字母、数字、下划线,长度 1-64", http.StatusBadRequest)
|
|
ErrDuplicateModel = NewAppError("duplicate_model", "同一供应商下模型名称已存在", http.StatusConflict)
|
|
ErrImmutableField = NewAppError("immutable_field", "供应商 ID 不允许修改", http.StatusBadRequest)
|
|
)
|
|
|
|
// AsAppError 尝试将 error 转换为 *AppError
|
|
func AsAppError(err error) (*AppError, bool) {
|
|
if err == nil {
|
|
return nil, false
|
|
}
|
|
var appErr *AppError
|
|
if ok := is(err, &appErr); ok {
|
|
return appErr, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func is(err error, target interface{}) bool {
|
|
// 简单的类型断言
|
|
if e, ok := err.(*AppError); ok {
|
|
// 直接赋值
|
|
switch t := target.(type) {
|
|
case **AppError:
|
|
*t = e
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|