Documentation
¶
Index ¶
- Constants
- func As(err error, target interface{}) bool
- func Code(err error) int
- func Is(err, target error) bool
- func Reason(err error) string
- func Unwrap(err error) error
- type XError
- func (err *XError) Error() string
- func (err *XError) GRPCStatus() *status.Status
- func (err *XError) Is(target error) bool
- func (err *XError) KV(kvs ...string) *XError
- func (err *XError) WithMessage(format string, args ...any) *XError
- func (err *XError) WithMetadata(md map[string]string) *XError
- func (err *XError) WithRequestID(requestID string) *XError
Constants ¶
const ( // UnknownCode 定义了未知错误的代码. UnknownCode = http.StatusInternalServerError // UnknownReason 定义了未知错误的原因. UnknownReason = "" )
Variables ¶
This section is empty.
Functions ¶
func As ¶
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.
As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.
func Is ¶
Is reports whether any error in err's chain matches target.
The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.
An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.
Types ¶
type XError ¶
type XError struct { // Code 表示错误的 HTTP 状态码,用于与客户端进行交互时标识错误的类型。 Code int `json:"code,omitempty"` // Reason 表示错误发生的原因,通常为业务错误码,用于精准定位问题。 Reason string `json:"reason,omitempty"` // Message 表示简短的错误信息,通常可直接暴露给用户查看。 Message string `json:"message,omitempty"` // Metadata 用于存储与该错误相关的额外元信息,可以包含上下文或调试信息。 Metadata map[string]string `json:"metadata,omitempty"` }
XError 定义了 OneX 项目体系中使用的错误类型,用于描述错误的详细信息。
func (*XError) WithMessage ¶
WithMessage 设置错误的 Message 字段.
func (*XError) WithMetadata ¶
WithMetadata 设置元数据.
func (*XError) WithRequestID ¶
WithRequestID 设置请求 ID.