xerrors

package
v0.2.0-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 6 Imported by: 0

README

This package provides a convenient way to map custom errors to both HTTP and gRPC status codes. This helps in ensuring consistency in error handling across different service architectures.

Documentation

Index

Constants

View Source
const (
	// UnknownCode 定义了未知错误的代码.
	UnknownCode = http.StatusInternalServerError
	// UnknownReason 定义了未知错误的原因.
	UnknownReason = ""
)

Variables

This section is empty.

Functions

func As

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

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 Code

func Code(err error) int

Code 返回错误的 HTTP 代码.

func Is

func Is(err, target error) bool

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.

func Reason

func Reason(err error) string

Reason 返回特定错误的原因.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

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 FromError

func FromError(err error) *XError

FromError 尝试将一个通用的 error 转换为自定义的 *XError 类型.

func New

func New(code int, reason string, format string, args ...any) *XError

New 创建一个新的错误.

func (*XError) Error

func (err *XError) Error() string

Error 实现 error 接口中的 `Error` 方法.

func (*XError) GRPCStatus

func (err *XError) GRPCStatus() *status.Status

GRPCStatus 返回 gRPC 状态表示.

func (*XError) Is

func (err *XError) Is(target error) bool

Is matches each error in the chain with the target value.

func (*XError) KV

func (err *XError) KV(kvs ...string) *XError

KV 使用 key-value 对设置元数据.

func (*XError) WithMessage

func (err *XError) WithMessage(format string, args ...any) *XError

WithMessage 设置错误的 Message 字段.

func (*XError) WithMetadata

func (err *XError) WithMetadata(md map[string]string) *XError

WithMetadata 设置元数据.

func (*XError) WithRequestID

func (err *XError) WithRequestID(requestID string) *XError

WithRequestID 设置请求 ID.

Jump to

Keyboard shortcuts

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