Documentation
¶
Overview ¶
Package errorutils 提供错误处理相关的工具函数 Package errorutils provides error handling utility functions
Index ¶
- func FormatError(err error, includeStack bool) string
- func IsCode(err error, code ErrorCode) bool
- func IsType(err error, errType ErrorType) bool
- func NewWithType(errType ErrorType, code ErrorCode, message string) error
- func StackTrace(err error) []string
- func WithStack(err error) error
- func Wrap(err error, message string) error
- func WrapWithType(err error, errType ErrorType, code ErrorCode, message string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type ErrorCode
- type ErrorType
- type WrappedError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatError ¶
FormatError 格式化错误消息,包含堆栈跟踪
参数 / Parameters:
- err: 错误 / error
- includeStack: 是否包含堆栈跟踪 / whether to include stack trace
返回值 / Returns:
- string: 格式化后的错误消息 / formatted error message
示例 / Example:
formatted := FormatError(err, true)
FormatError formats an error message with optional stack trace
func IsCode ¶
IsCode 检查错误是否为指定代码
参数 / Parameters:
- err: 错误 / error
- code: 要检查的错误代码 / error code to check
返回值 / Returns:
- bool: 如果错误是指定代码则返回true / true if error is of the specified code
示例 / Example:
if IsCode(err, ErrorCodeInvalidInput) { ... }
IsCode checks if an error is of a specific code
func IsType ¶
IsType 检查错误是否为指定类型
参数 / Parameters:
- err: 错误 / error
- errType: 要检查的错误类型 / error type to check
返回值 / Returns:
- bool: 如果错误是指定类型则返回true / true if error is of the specified type
示例 / Example:
if IsType(err, ErrorTypeValidation) { ... }
IsType checks if an error is of a specific type
func NewWithType ¶
NewWithType 创建一个带类型的错误
参数 / Parameters:
- errType: 错误类型 / error type
- code: 错误代码 / error code
- message: 错误消息 / error message
返回值 / Returns:
- error: 新创建的错误 / newly created error
示例 / Example:
err := NewWithType(ErrorTypeValidation, ErrorCodeInvalidInput, "invalid input")
NewWithType creates a new error with type and code
func StackTrace ¶
StackTrace 获取错误的堆栈跟踪
参数 / Parameters:
- err: 错误 / error
返回值 / Returns:
- []string: 堆栈跟踪信息,如果没有则返回空切片 / stack trace, empty slice if not available
示例 / Example:
stack := StackTrace(err)
StackTrace returns the stack trace of an error
func WithStack ¶
WithStack 为错误添加堆栈跟踪
参数 / Parameters:
- err: 要添加堆栈的错误,如果为nil则返回nil / error to add stack to, returns nil if err is nil
返回值 / Returns:
- error: 带有堆栈跟踪的错误 / error with stack trace
示例 / Example:
err := errors.New("some error")
errWithStack := WithStack(err)
WithStack adds stack trace to an error
func Wrap ¶
Wrap 包装一个错误,添加消息和堆栈跟踪
参数 / Parameters:
- err: 要包装的错误,如果为nil则返回nil / error to wrap, returns nil if err is nil
- message: 错误消息 / error message
返回值 / Returns:
- error: 包装后的错误 / wrapped error
示例 / Example:
err := errors.New("original error")
wrapped := Wrap(err, "failed to process")
Wrap wraps an error with a message and stack trace
func WrapWithType ¶
WrapWithType 包装错误并指定类型和代码
参数 / Parameters:
- err: 要包装的错误 / error to wrap
- errType: 错误类型 / error type
- code: 错误代码 / error code
- message: 错误消息 / error message
返回值 / Returns:
- error: 包装后的错误 / wrapped error
示例 / Example:
err := errors.New("original error")
wrapped := WrapWithType(err, ErrorTypeValidation, ErrorCodeInvalidInput, "validation failed")
WrapWithType wraps an error with type and code
func Wrapf ¶
Wrapf 使用格式化字符串包装错误
参数 / Parameters:
- err: 要包装的错误,如果为nil则返回nil / error to wrap, returns nil if err is nil
- format: 格式化字符串 / format string
- args: 格式化参数 / format arguments
返回值 / Returns:
- error: 包装后的错误 / wrapped error
示例 / Example:
err := errors.New("original error")
wrapped := Wrapf(err, "failed to process %s", "file.txt")
Wrapf wraps an error with a formatted message
Types ¶
type ErrorCode ¶
type ErrorCode int
ErrorCode 错误代码 ErrorCode represents error code
const ( // ErrorCodeUnknown 未知错误代码 // ErrorCodeUnknown represents unknown error code ErrorCodeUnknown ErrorCode = 0 // ErrorCodeInvalidInput 无效输入错误代码 // ErrorCodeInvalidInput represents invalid input error code ErrorCodeInvalidInput ErrorCode = 1000 // ErrorCodeNotFound 未找到错误代码 // ErrorCodeNotFound represents not found error code ErrorCodeNotFound ErrorCode = 1001 // ErrorCodeUnauthorized represents unauthorized error code ErrorCodeUnauthorized ErrorCode = 1002 // ErrorCodeForbidden 禁止访问错误代码 // ErrorCodeForbidden represents forbidden error code ErrorCodeForbidden ErrorCode = 1003 // ErrorCodeTimeout 超时错误代码 // ErrorCodeTimeout represents timeout error code ErrorCodeTimeout ErrorCode = 1004 // ErrorCodeInternal 内部错误代码 // ErrorCodeInternal represents internal error code ErrorCodeInternal ErrorCode = 5000 )
type ErrorType ¶
type ErrorType string
ErrorType 错误类型 ErrorType represents the type of error
const ( // ErrorTypeUnknown 未知错误类型 // ErrorTypeUnknown represents unknown error type ErrorTypeUnknown ErrorType = "unknown" // ErrorTypeValidation 验证错误类型 // ErrorTypeValidation represents validation error type ErrorTypeValidation ErrorType = "validation" // ErrorTypeNotFound 未找到错误类型 // ErrorTypeNotFound represents not found error type ErrorTypeNotFound ErrorType = "not_found" // ErrorTypePermission 权限错误类型 // ErrorTypePermission represents permission error type ErrorTypePermission ErrorType = "permission" // ErrorTypeNetwork 网络错误类型 // ErrorTypeNetwork represents network error type ErrorTypeNetwork ErrorType = "network" // ErrorTypeTimeout 超时错误类型 // ErrorTypeTimeout represents timeout error type ErrorTypeTimeout ErrorType = "timeout" // ErrorTypeInternal 内部错误类型 // ErrorTypeInternal represents internal error type ErrorTypeInternal ErrorType = "internal" )
type WrappedError ¶
type WrappedError struct {
// Err 原始错误 / original error
Err error
// Message 错误消息 / error message
Message string
// Stack 堆栈跟踪 / stack trace
Stack []string
// Type 错误类型 / error type
Type ErrorType
// Code 错误代码 / error code
Code ErrorCode
}
WrappedError 包装的错误,包含原始错误、消息和堆栈信息 WrappedError wraps an error with message and stack trace
func (*WrappedError) Error ¶
func (e *WrappedError) Error() string
Error 实现 error 接口 Error implements the error interface
func (*WrappedError) Unwrap ¶
func (e *WrappedError) Unwrap() error
Unwrap 返回原始错误,用于 errors.Unwrap Unwrap returns the original error for errors.Unwrap