errors

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: BSD-3-Clause Imports: 3 Imported by: 10

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// 配置相关错误
	ErrProfileNotFound               = errors.New("profile not found")
	ErrInvalidFingerprint            = errors.New("invalid fingerprint format")
	ErrClientHelloSpecNotImplemented = errors.New("client hello spec not implemented")
	ErrInvalidUserAgent              = errors.New("invalid user agent")
	ErrNoProfilesAvailable           = errors.New("no profiles available")
	ErrUnsupportedBrowser            = errors.New("unsupported browser")

	// 配置中心错误
	ErrConfigNotLoaded     = errors.New("config not loaded")
	ErrConfigValidation    = errors.New("config validation failed")
	ErrConfigPathNotFound  = errors.New("config path not found")
	ErrConfigAlreadyExists = errors.New("config already exists")
	ErrVersionNotFound     = errors.New("version not found")

	// 网络相关错误
	ErrInvalidIP        = errors.New("invalid IP address")
	ErrInvalidPort      = errors.New("invalid port")
	ErrConnectionFailed = errors.New("connection failed")

	// 协议相关错误
	ErrInvalidTLSVersion  = errors.New("invalid TLS version")
	ErrInvalidCipherSuite = errors.New("invalid cipher suite")
	ErrInvalidExtension   = errors.New("invalid extension")

	// 输入验证错误
	ErrInvalidInput  = errors.New("invalid input")
	ErrEmptyInput    = errors.New("empty input")
	ErrInputTooLarge = errors.New("input too large")
	ErrRequiredField = errors.New("required field is missing")

	// 内部错误
	ErrInternal       = errors.New("internal error")
	ErrNotImplemented = errors.New("not implemented")
	ErrTimeout        = errors.New("operation timeout")
	ErrCancelled      = errors.New("operation cancelled")
)

Functions

func As

func As(err error, target interface{}) bool

As 类型断言(兼容标准库)

func Is

func Is(err, target error) bool

Is 检查错误是否匹配(兼容标准库)

func IsClientHelloSpecNotImplemented

func IsClientHelloSpecNotImplemented(err error) bool

IsClientHelloSpecNotImplemented 判断错误是否表示 profile 未实现 ClientHelloSpec。

func IsCode added in v1.0.6

func IsCode(err error, code ErrorCode) bool

IsCode 检查错误是否匹配指定错误码

func IsConfigError

func IsConfigError(err error) bool

IsConfigError 检查是否为配置类错误

func IsInvalidInput

func IsInvalidInput(err error) bool

IsInvalidInput 检查是否为"无效输入"类错误

func IsNotFound

func IsNotFound(err error) bool

IsNotFound 检查是否为"未找到"类错误

func New

func New(text string) error

New 创建新错误(兼容标准库)

func NewConfigError

func NewConfigError(op string, err error) error

NewConfigError 创建配置错误

func NewNetworkError

func NewNetworkError(op string, err error) error

NewNetworkError 创建网络错误

func NewNotFoundError

func NewNotFoundError(resource string, identifier string) error

NewNotFoundError 创建未找到错误

func NewProtocolError

func NewProtocolError(protocol string, err error) error

NewProtocolError 创建协议错误

func NewValidationError

func NewValidationError(field string, reason string) error

NewValidationError 创建验证错误

func Newf

func Newf(format string, args ...interface{}) error

Newf 使用格式化字符串创建错误

func Wrap

func Wrap(err error, context string) error

Wrap 包装错误并添加上下文

Example

ExampleWrap 示例:错误包装

baseErr := errors.New("file not found")
wrapped := Wrap(baseErr, "loading config")
fmt.Println(wrapped)
Output:
loading config: file not found

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf 使用格式化字符串包装错误

Types

type CategorizedError

type CategorizedError struct {
	Category ErrorCategory
	Err      error
	Details  string
}

CategorizedError 带分类的错误

func NewCategorizedError

func NewCategorizedError(category ErrorCategory, err error, details string) *CategorizedError

NewCategorizedError 创建带分类的错误

Example

ExampleNewCategorizedError 示例:分类错误

err := NewCategorizedError(CategoryConfig, ErrConfigNotLoaded, "missing file")
fmt.Println(err)
Output:
[config] config not loaded: missing file

func (*CategorizedError) Error

func (e *CategorizedError) Error() string

func (*CategorizedError) Unwrap

func (e *CategorizedError) Unwrap() error

type CodeError added in v1.0.6

type CodeError struct {
	Code    ErrorCode
	Message string
	Cause   error
	Details map[string]any
}

CodeError 带错误码的错误

func ConfigNotLoaded added in v1.0.6

func ConfigNotLoaded(path string) *CodeError

ConfigNotLoaded 创建配置未加载错误

func Internal added in v1.0.6

func Internal(op string, cause error) *CodeError

Internal 创建内部错误

func InvalidInput added in v1.0.6

func InvalidInput(field string, reason string) *CodeError

InvalidInput 创建输入无效错误

func NewError added in v1.0.6

func NewError(code ErrorCode, message string) *CodeError

NewError 创建带错误码的错误

func NewErrorWithCause added in v1.0.6

func NewErrorWithCause(code ErrorCode, message string, cause error) *CodeError

NewErrorWithCause 创建带错误码和原因的错误

func ProfileInvalid added in v1.0.6

func ProfileInvalid(id string, reason string) *CodeError

ProfileInvalid 创建 Profile 无效错误

func ProfileNotFound added in v1.0.6

func ProfileNotFound(id string) *CodeError

ProfileNotFound 创建 Profile 不存在错误

func ProfileRemoveDefault added in v1.0.6

func ProfileRemoveDefault(id string) *CodeError

ProfileRemoveDefault 创建不能删除默认 Profile 错误

func RequiredField added in v1.0.6

func RequiredField(field string) *CodeError

RequiredField 创建必填字段缺失错误

func Timeout added in v1.0.6

func Timeout(op string, duration any) *CodeError

Timeout 创建超时错误

func (*CodeError) Error added in v1.0.6

func (e *CodeError) Error() string

func (*CodeError) Unwrap added in v1.0.6

func (e *CodeError) Unwrap() error

func (*CodeError) WithDetail added in v1.0.6

func (e *CodeError) WithDetail(key string, value any) *CodeError

WithDetail 添加详细信息

type ErrorCategory

type ErrorCategory string

ErrorCategory 错误分类

const (
	CategoryConfig   ErrorCategory = "config"
	CategoryNetwork  ErrorCategory = "network"
	CategoryProtocol ErrorCategory = "protocol"
	CategoryInput    ErrorCategory = "input"
	CategoryInternal ErrorCategory = "internal"
	CategoryNotFound ErrorCategory = "not_found"
)

type ErrorCode added in v1.0.6

type ErrorCode string

ErrorCode 错误码类型

const (
	// 系统级错误 (SYS)
	ErrCodeSystem     ErrorCode = "SYS001"
	ErrCodeNotFound   ErrorCode = "SYS002"
	ErrCodeInvalidArg ErrorCode = "SYS003"
	ErrCodeTimeout    ErrorCode = "SYS004"
	ErrCodeCancelled  ErrorCode = "SYS005"
	ErrCodeInternal   ErrorCode = "SYS006"
	ErrCodeNotImpl    ErrorCode = "SYS007"

	// Profile 相关错误 (PRF)
	ErrCodeProfileNotFound      ErrorCode = "PRF001"
	ErrCodeProfileInvalid       ErrorCode = "PRF002"
	ErrCodeProfileExists        ErrorCode = "PRF003"
	ErrCodeProfileLoadFailed    ErrorCode = "PRF004"
	ErrCodeProfileSaveFailed    ErrorCode = "PRF005"
	ErrCodeProfileNoDefault     ErrorCode = "PRF006"
	ErrCodeProfileRemoveDefault ErrorCode = "PRF007"

	// 配置相关错误 (CFG)
	ErrCodeConfigNotLoaded    ErrorCode = "CFG001"
	ErrCodeConfigValidation   ErrorCode = "CFG002"
	ErrCodeConfigPathNotFound ErrorCode = "CFG003"
	ErrCodeConfigExists       ErrorCode = "CFG004"
	ErrCodeConfigVersion      ErrorCode = "CFG005"

	// 网络相关错误 (NET)
	ErrCodeNetInvalidIP   ErrorCode = "NET001"
	ErrCodeNetInvalidPort ErrorCode = "NET002"
	ErrCodeNetConnect     ErrorCode = "NET003"
	ErrCodeNetTimeout     ErrorCode = "NET004"

	// 协议相关错误 (PRT)
	ErrCodeProtoTLSVersion  ErrorCode = "PRT001"
	ErrCodeProtoCipherSuite ErrorCode = "PRT002"
	ErrCodeProtoExtension   ErrorCode = "PRT003"
	ErrCodeProtoInvalidSpec ErrorCode = "PRT004"

	// 输入验证错误 (INP)
	ErrCodeInputInvalid  ErrorCode = "INP001"
	ErrCodeInputEmpty    ErrorCode = "INP002"
	ErrCodeInputTooLarge ErrorCode = "INP003"
	ErrCodeInputRequired ErrorCode = "INP004"

	// 缓存相关错误 (CCH)
	ErrCodeCacheNotFound ErrorCode = "CCH001"
	ErrCodeCacheExpired  ErrorCode = "CCH002"

	// ML 相关错误 (ML)
	ErrCodeMLModelNotFound ErrorCode = "ML001"
	ErrCodeMLPrediction    ErrorCode = "ML002"

	// 插件相关错误 (PLG)
	ErrCodePluginNotFound ErrorCode = "PLG001"
	ErrCodePluginInit     ErrorCode = "PLG002"
)

func GetCode added in v1.0.6

func GetCode(err error) ErrorCode

GetCode 获取错误码

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL