Documentation
¶
Index ¶
- Variables
- func GetContext(err error) map[string]interface{}
- func GetHTTPStatus(err error) int
- func GetMessage(err error) string
- func HasHTTPStatus(err error) bool
- func IsCode(err error, code ErrorCode) bool
- func IsRetryable(err error) bool
- type Error
- func New(code ErrorCode, message string) *Error
- func NewWithHTTPStatus(code ErrorCode, message string, status int) *Error
- func Wrap(code ErrorCode, message string, cause error) *Error
- func WrapWithContext(code ErrorCode, message string, cause error, context map[string]interface{}) *Error
- func WrapWithHTTPStatus(code ErrorCode, message string, cause error, status int) *Error
- func WrapWithHTTPStatusAndContext(code ErrorCode, message string, cause error, status int, ...) *Error
- type ErrorCode
- type ErrorLevel
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // 通用错误 ErrUnknown = New(ErrCodeUnknown, "未知错误") ErrInternal = New(ErrCodeInternal, "内部错误") ErrInvalidArgument = New(ErrCodeInvalidArgument, "无效参数") ErrNotFound = New(ErrCodeNotFound, "资源未找到") ErrPermissionDenied = New(ErrCodePermissionDenied, "权限被拒绝") ErrAuthenticationFailed = New(ErrCodeAuthenticationFailed, "认证失败") ErrResourceExhausted = New(ErrCodeResourceExhausted, "资源耗尽") ErrFailedPrecondition = New(ErrCodeFailedPrecondition, "前置条件失败") ErrAborted = New(ErrCodeAborted, "操作中止") ErrOutOfRange = New(ErrCodeOutOfRange, "超出范围") ErrUnimplemented = New(ErrCodeUnimplemented, "功能未实现") ErrDataLoss = New(ErrCodeDataLoss, "数据丢失") ErrDeadlineExceeded = New(ErrCodeDeadlineExceeded, "超过截止时间") // 业务错误 ErrNoHealthyChannel = New(ErrCodeNoHealthyChannel, "没有可用的健康通道") ErrAdapterNotFound = New(ErrCodeAdapterNotFound, "未找到适配器") ErrModelNotFound = New(ErrCodeModelNotFound, "未找到模型") ErrPlatformNotFound = New(ErrCodePlatformNotFound, "未找到平台") ErrEndpointNotFound = New(ErrCodeEndpointNotFound, "未找到端点") ErrAPIKeyNotFound = New(ErrCodeAPIKeyNotFound, "未找到 API 密钥") ErrRequestFailed = New(ErrCodeRequestFailed, "请求失败") ErrStreamError = New(ErrCodeStreamError, "流处理错误") ErrConfigInvalid = New(ErrCodeConfigInvalid, "配置无效") ErrRateLimitExceeded = New(ErrCodeRateLimitExceeded, "超出速率限制") ErrCircuitBreakerOpen = New(ErrCodeCircuitBreakerOpen, "熔断器已开启") ErrEmptyResponse = New(ErrCodeEmptyResponse, "响应为空") )
预定义的常用错误
View Source
var As = errors.As
As 是标准库 errors.As 的别名,用于错误类型断言
View Source
var RetryableErrorCodes = map[ErrorCode]bool{ ErrCodeUnavailable: true, ErrCodeStreamError: true, ErrCodeUnimplemented: true, }
RetryableErrorCodes 定义可重试的错误码集合
Functions ¶
func IsRetryable ¶ added in v0.5.0
IsRetryable 检查错误是否可重试 策略: - 如果错误来源于供应商或上游 (具有 error_from 字段),全都允许重试 - 如果来源于网关自身 (不存在 error_from 字段),按照可重试的白名单进行返回
Types ¶
type Error ¶
type Error struct {
Code ErrorCode // 错误码
HTTPStatus *int // HTTP 状态码
Message string // 错误消息
Cause error // 原始错误
Context map[string]interface{} // 上下文信息
}
Error 结构化错误类型
func NewWithHTTPStatus ¶
NewWithHTTPStatus 创建新的带 HTTP 状态码的错误
func WrapWithContext ¶
func WrapWithContext(code ErrorCode, message string, cause error, context map[string]interface{}) *Error
WrapWithContext 包装错误并添加上下文
func WrapWithHTTPStatus ¶
WrapWithHTTPStatus 包装已有错误并设置 HTTP 状态码
func WrapWithHTTPStatusAndContext ¶
func WrapWithHTTPStatusAndContext(code ErrorCode, message string, cause error, status int, context map[string]interface{}) *Error
WrapWithHTTPStatusAndContext 包装错误并添加 HTTP 状态码和上下文
func (*Error) WithContext ¶
WithContext 添加上下文信息
func (*Error) WithHTTPStatus ¶
WithHTTPStatus 设置 HTTP 状态码
type ErrorCode ¶
type ErrorCode string
ErrorCode 定义错误码类型
const ( // 未知错误 ErrCodeUnknown ErrorCode = "UNKNOWN" // 内部错误 ErrCodeInternal ErrorCode = "INTERNAL" // 无效参数 ErrCodeInvalidArgument ErrorCode = "INVALID_ARGUMENT" // 资源未找到 ErrCodeNotFound ErrorCode = "NOT_FOUND" // 权限拒绝 ErrCodePermissionDenied ErrorCode = "PERMISSION_DENIED" // 认证失败 ErrCodeAuthenticationFailed ErrorCode = "AUTHENTICATION_FAILED" // 资源耗尽 ErrCodeResourceExhausted ErrorCode = "RESOURCE_EXHAUSTED" // 前置条件失败 ErrCodeFailedPrecondition ErrorCode = "FAILED_PRECONDITION" // 操作中止 ErrCodeAborted ErrorCode = "ABORTED" // 超出范围 ErrCodeOutOfRange ErrorCode = "OUT_OF_RANGE" // 未实现 ErrCodeUnimplemented ErrorCode = "UNIMPLEMENTED" ErrCodeUnavailable ErrorCode = "UNAVAILABLE" // 数据丢失 ErrCodeDataLoss ErrorCode = "DATA_LOSS" // 超时 ErrCodeDeadlineExceeded ErrorCode = "DEADLINE_EXCEEDED" // 无健康通道 ErrCodeNoHealthyChannel ErrorCode = "NO_HEALTHY_CHANNEL" // 适配器未找到 ErrCodeAdapterNotFound ErrorCode = "ADAPTER_NOT_FOUND" // 模型未找到 ErrCodeModelNotFound ErrorCode = "MODEL_NOT_FOUND" // 平台未找到 ErrCodePlatformNotFound ErrorCode = "PLATFORM_NOT_FOUND" // 端点未找到 ErrCodeEndpointNotFound ErrorCode = "ENDPOINT_NOT_FOUND" // API 密钥未找到 ErrCodeAPIKeyNotFound ErrorCode = "API_KEY_NOT_FOUND" // 请求失败 ErrCodeRequestFailed ErrorCode = "REQUEST_FAILED" // 流处理错误 ErrCodeStreamError ErrorCode = "STREAM_ERROR" // 配置无效 ErrCodeConfigInvalid ErrorCode = "CONFIG_INVALID" // 超出速率限制 ErrCodeRateLimitExceeded ErrorCode = "RATE_LIMIT_EXCEEDED" // 熔断器开启 ErrCodeCircuitBreakerOpen ErrorCode = "CIRCUIT_BREAKER_OPEN" // 响应为空 ErrCodeEmptyResponse ErrorCode = "EMPTY_RESPONSE" )
预定义的错误码
type ErrorLevel ¶ added in v0.5.0
type ErrorLevel int
ErrorLevel 定义错误层级类型
const ( // ErrorLevelPlatform 平台层级错误 ErrorLevelPlatform ErrorLevel = iota // ErrorLevelKey 密钥层级错误 ErrorLevelKey // ErrorLevelModel 模型层级错误 ErrorLevelModel )
func GetErrorLevel ¶ added in v0.5.0
func GetErrorLevel(err error) ErrorLevel
GetErrorLevel 根据错误获取错误层级
Click to show internal directories.
Click to hide internal directories.