Documentation
¶
Index ¶
- func As(err error, target any) bool
- func DefaultErrCode(code int)
- func DefaultStackEnabled(enabled bool)
- func Is(err, target error) bool
- func Join(errs ...error) error
- func SetJsoner(lib JsonerAPI)
- func Unwrap(err error) error
- type Caller
- type Error
- func (e *Error) Code() int
- func (e *Error) Error() string
- func (e *Error) Format(s fmt.State, verb rune)
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) Source() (source Source, exists bool)
- func (e *Error) Stacks() []Source
- func (e *Error) String() string
- func (e *Error) UnmarshalJSON(data []byte) error
- func (e *Error) Unwrap() error
- func (e *Error) WithLogger(logs ...*slog.Logger)
- func (e *Error) WithStack(enabled bool) IError
- type IError
- func Defined(msg string) IError
- func DefinedCode(code int, msg string) IError
- func DefinedCodeF(code int, format string, a ...any) IError
- func DefinedF(format string, a ...any) IError
- func Errorf(format string, a ...any) IError
- func HttpStatusWith(statusCode int) IError
- func InvalidParamWith(params ...string) IError
- func InvalidValueFormat(format string, a ...any) IError
- func MissingParamsWith(params ...string) IError
- func New(msg string) IError
- func NewCode(code int, msg string) IError
- func NewCodeF(code int, format string, a ...any) IError
- func NewF(format string, a ...any) IError
- func Wrap(err error, msg string) IError
- func WrapCode(code int, err error, msg string) IError
- func WrapCodeF(code int, err error, format string, a ...any) IError
- func WrapF(err error, format string, a ...any) IError
- func WrapOrNew(err error, msg string) IError
- func WrapOrNewCode(code int, err error, msg string) IError
- func WrapOrNewCodeF(code int, err error, format string, a ...any) IError
- func WrapOrNewF(err error, format string, a ...any) IError
- type JsonerAPI
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As 尝试将错误转换为特定的错误类型(错误类型转换)——基于类型断言机制/类型选择机制
- 尝试将 error 或其包装的任何错误转换为 target 指向的类型
- 用于获取特定类型的错误以访问其方法或字段
- 类似于类型断言 error.(T),但能穿透错误包装
- 返回 true: 表示成功找到匹配的错误类型,并已将值存入 target
- 返回 false: 表示未找到匹配的错误类型
func DefaultStackEnabled ¶
func DefaultStackEnabled(enabled bool)
DefaultStackEnabled 设置默认是否记录Stack的选项, 默认为False
Types ¶
type Caller ¶
type Caller []uintptr // 获取的堆栈指针切片(注意: 序列化时指针无效做丢弃处理)
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
func (*Error) WithLogger ¶
WithLogger 手动将错误信息登记到指定的slog日志记录器
type IError ¶
type IError interface {
// Code 获取错误码
Code() int
// Error 获取错误信息(不包含堆栈信息)
Error() string
// String 获取详细的错误信息(如存在堆栈信息则包含)
String() string
// Unwrap 获取被包装的错误, 如果没有被包装,则返回 error<nil>
Unwrap() error
// WithStack 手动关闭或启用(更新)堆栈信息
// - enabled 为 false 时,关闭堆栈信息
// - enabled 为 true 时,启用(更新)堆栈信息
// - - !isDefined 非预定义错误对象,如不存在堆栈信息则开启堆栈,否则不做任何操作
// - - isDefined 预定义错误对象,创建一个错误对象的副本并启用堆栈信息
WithStack(enabled bool) IError
// WithLogger 手动将错误信息登记到指定的slog日志记录器
WithLogger(logs ...*slog.Logger)
// Source 获取Caller堆栈Source信息(第一条堆栈信息)
// - source 返回值: 获取成功时返回Source对象
// - exists 返回值: 未开启堆栈或获取失败时返回 false
Source() (source Source, exists bool)
// Stacks 获取Caller堆栈的[]Source信息切片
// - 返回值: 未开启堆栈或获取失败时返回 nil
Stacks() []Source
// Format FMT格式化输出接口
Format(s fmt.State, verb rune)
// MarshalJSON 序列化接口 encoding/json.Marshaler
MarshalJSON() ([]byte, error)
// UnmarshalJSON 反序列化接口 encoding/json.Unmarshaler
UnmarshalJSON(data []byte) error
}
func Defined ¶
- Defined 预定义方法 可以在项目中预定义一组常用的错误信息
- -------------------------------
- 错误码通常设计为6位,前3位为模块标识或HTTP状态码,后3位为自定义错误码
- 自定义请求/响应类错误码:
- -------------------------------
- 请求类错误 400XXX => HTTP 400 (自定义范围: 400001 ~ 400999)
- 认证类错误 401XXX => HTTP 401 (自定义范围: 401001 ~ 401999)
- 拒绝类错误 403XXX => HTTP 403、404 (自定义范围: 403001 ~ 404999)
- 服务类错误 500XXX => HTTP 500 (自定义范围: 500001 ~ 500999)
- -------------------------------
- 自定义业务类错误码:
- 前3位为模块标识,后3位为自定义错误码
- 自定义错误 600XXX => HTTP 400/500 (自定义范围: 600001 ~ 999999)
Defined 预定义一个 Error 错误
- msg 错误信息
- 注意: 预定义错误没有堆栈信息,如需堆栈信息,程序返回预定义错误时请调用 <GerrorObj>.WithStack(true)
func DefinedCode ¶
DefinedCode 预定义一个 Error 错误
- code 错误码
- msg 错误信息
- 注意: 预定义错误没有堆栈信息,如需堆栈信息,程序返回预定义错误时请调用 <GerrorObj>.WithStack(true)
func DefinedCodeF ¶
DefinedCodeF 预定义一个格式化的 Error 错误, like fmt.Errorf
- code 错误码
- msg 错误信息
- 注意: 预定义错误没有堆栈信息,如需堆栈信息,程序返回预定义错误时请调用 <GerrorObj>.WithStack(true)
func DefinedF ¶
DefinedF 预定义一个 Error 错误, like fmt.Errorf
- msg 错误信息
- 注意: 预定义错误没有堆栈信息,如需堆栈信息,程序返回预定义错误时请调用 <GerrorObj>.WithStack(true)
func HttpStatusWith ¶
HttpStatusWith HTTP Status Code error
- No Defined Error, stack options: use[defaultStackEnabled]
func InvalidParamWith ¶
InvalidParamWith 自定义某个或多个参数错误
- params 参数名称清单
- return Error{code<defaultErrCode>,msg<"invalid params: x,x...">}
- No Defined Error, stack options: use[defaultStackEnabled]
func InvalidValueFormat ¶
InvalidValueFormat 自定义某个参数值错误信息
- format, a ...any fmt 格式化方式自定义错误信息
- return Error{code<defaultErrCode>,msg<"invalid value: format string">}
- No Defined Error, stack options: use[defaultStackEnabled]
func MissingParamsWith ¶
MissingParamsWith 自定义缺少某个或多个必要的参数错误
- params 参数名称清单
- return Error{code<defaultErrCode>,msg<"missing params: x,x...">}
- No Defined Error, stack options: use[defaultStackEnabled]
func WrapOrNew ¶ added in v1.0.2
WrapOrNew 包裹err或创建一个 Error 错误
- err 为 nil 时,创建一个 Error 错误
- err 不为 nil 时,包裹err创建一个 Error 错误
func WrapOrNewCode ¶ added in v1.0.2
WrapOrNewCode 包裹err或创建一个含有Code的 Error 错误
- err 为 nil 时,创建一个 Error 错误
- err 不为 nil 时,包裹err创建一个 Error 错误
func WrapOrNewCodeF ¶ added in v1.0.2
WrapOrNewCodeF 包裹err或创建一个含有Code的格式化消息的 Error 错误
- err 为 nil 时,创建一个 Error 错误
- err 不为 nil 时,包裹err创建一个 Error 错误
type JsonerAPI ¶
type JsonerAPI interface {
// Marshal adapts to json/encoding Marshal API
// Refer to https://godoc.org/encoding/json#Marshal for more information
Marshal(v any) ([]byte, error)
// Unmarshal adapts to json/encoding Unmarshal API
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
Unmarshal(data []byte, v any) error
}
type Source ¶
func (Source) Format ¶
Format Source formats the frame according to the fmt.Formatter interface.
%s source file %d source line %n function name %v equivalent to %s:%d
Format accepts flags that alter the printing of some verbs, as follows:
%+s function name and path of source file relative to the compile time
GOPATH separated by \n\t (<funcname>\n\t<path>)
%+v equivalent to %+s:%d