errors

package
v0.0.0-...-f6e3e14 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target *Error) bool

As 尝试将错误转换为StatusError

func GetHTTPStatus

func GetHTTPStatus(errType ErrorType) int

func Is

func Is(err error, errorType ErrorType) bool

Is 检查错误是否为指定的错误类型

func RegisterHTTPStatusMapping

func RegisterHTTPStatusMapping(errType ErrorType, httpStatus int)

func Unwarp

func Unwarp(err error) error

Types

type Error

type Error struct {
	Type       ErrorType         `json:"type,omitempty"`
	Code       int               `json:"code,omitempty"`
	Msg        string            `json:"message,omitempty"`
	Reason     error             `json:"-"`
	MetaKV     map[string]string `json:"meta,omitempty"`
	StackTrace string            `json:"stack_trace,omitempty"`
}

func New

func New(errorType ErrorType, code int, msg string, args ...any) *Error

func Wrap

func Wrap(err error, errorType ErrorType, code int, msg string, args ...any) *Error

func (*Error) AppendKVs

func (e *Error) AppendKVs(kvs ...string) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) HttpStatus

func (e *Error) HttpStatus() int

func (*Error) ToJSON

func (e *Error) ToJSON() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) WithCode

func (e *Error) WithCode(code int) *Error

func (*Error) WithMeta

func (e *Error) WithMeta(mp map[string]string) *Error

func (*Error) WithReason

func (e *Error) WithReason(err error) *Error

type ErrorType

type ErrorType string
const (
	ErrUnknown       ErrorType = "Unkonwn"
	ErrInvalid       ErrorType = "Invalid"
	ErrNotFound      ErrorType = "Not Found"
	ErrAlreadyExists ErrorType = "Already Exists"
	ErrUnauthorized  ErrorType = "Unauthorized"
	ErrForbidden     ErrorType = "Forbidden"
	ErrUnavailable   ErrorType = "Unavailable"
	ErrTimeout       ErrorType = "Timeout"
	ErrInternal      ErrorType = "Internal"
)

Jump to

Keyboard shortcuts

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