errorx

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 16 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// ClientClosed is non-standard http status code,
	// which defined by nginx.
	// https://httpstatus.in/499/
	ClientClosed = 499
)

Variables

View Source
var File_pkg_errorx_errorx_proto protoreflect.FileDescriptor

Functions

func Cause

func Cause(err error) error

Cause 返回调用链最底层错误的根因

func CreateBaseErrCode

func CreateBaseErrCode()

func Fatal

func Fatal(err error, msg ...string)

Fatal panic退出程序

func FromGRPCCode

func FromGRPCCode(code codes.Code) int

FromGRPCCode converts a gRPC error code into the corresponding HTTP response status.

func Stack

func Stack(err error) string

Stack 打印全部错误得堆栈信息

func ToGRPCCode

func ToGRPCCode(code int) codes.Code

ToGRPCCode converts an HTTP error code into the corresponding gRPC response status.

func Wrap

func Wrap(err error, format string, a ...interface{}) error

Wrap 携带携带上层error,赋值给Cause 根因后返回

func WrapErr

func WrapErr(err error, code ErrCode) error

WrapErr 原生err 携带 errCode 错误提示

Types

type BatchError

type BatchError struct {
	// contains filtered or unexported fields
}

A BatchError is an error that can hold multiple errors.

func (*BatchError) Add

func (be *BatchError) Add(errs ...error)

Add adds errs to be, nil errors are ignored.

func (*BatchError) Err

func (be *BatchError) Err() error

Err returns an error that represents all errors.

func (*BatchError) NotNil

func (be *BatchError) NotNil() bool

NotNil checks if any error inside.

type Converter

type Converter interface {
	// ToGRPCCode converts an HTTP error code into the corresponding gRPC response status.
	ToGRPCCode(code int) codes.Code

	// FromGRPCCode converts a gRPC error code into the corresponding HTTP response status.
	FromGRPCCode(code codes.Code) int
}

Converter is a status converter.

var DefaultConverter Converter = statusConverter{}

type ErrCode

type ErrCode uint64
const (
	ErrCodeOK       ErrCode = errCodeBase + iota
	ErrCodeStandard         //默认缺少指定error-code类型
	ErrCodeJsonErr
	ErrCodeNotAuthorized
	ErrCodeBusy
	ErrCodeTimeOut
	ErrCodeParamsErr
	ErrCodeNotFound
	ErrCodeNetWorkErr
	ErrCodeUnknown
	ErrCodeLogPathNotSet
	ErrCodeLogFileClosed
	ErrCodeWatcherFileStop
	ErrCodeAssertionValue
	ErrCodeGetPathValue
	ErrCodeMsgTypeMismatch
	ErrCodeBaseMax = errCodeBase | errMask
)
const (
	ErrCodeRedisConnFailed ErrCode = errCodeRedis + iota
	ErrCodeRedisWaitCASTimeout
	ErrCodeRedisKeyIsEmpty
	ErrCodeRedisCacheNotMiss
	ErrCodeRedisCacheDelFailed
)
const (
	ErrCodeDbConnFailed ErrCode = errCodeDB + iota
	ErrCodeDBQueryFailed
	ErrCodeDBQueryCountRepeat
)
const (
	ErrCodeMqRabbitConnFailed ErrCode = errCodeMq + iota
	ErrCodeMqRedisConnFailed
	ErrCodeMqALLConnFailed
)
const (
	ErrRpcCodeParentGroupNotExist ErrCode = errCodeRpcServer + iota
	ErrRpcServerNotFound
	ErrRpcCodeGroupNotEmpty
	ErrRpcCodeConfIsBusy2
	ErrRpcCodeMobileRepeat
	ErrRpcCodeGroupIndepFailed
	ErrRpcCodeGroupLevelTooBig
	ErrRpcCodeCapacityTooBig
	ErrRpcCodeEmailRepeat
	ErrRpcCodeDelTopGroup
	ErrRpcCodeAgentMax = errCodeRpcServer | errMask
)
const (
	ErrApiCodeParentGroupNotExist ErrCode = errCodeApiServer + iota
	ErrApiCodeShouldBindJSON
	ErrApiCodeGroupNotEmpty
	ErrApiCodeConfIsBusy2
	ErrApiCodeMobileRepeat
	ErrApiCodeGroupIndepFailed
	ErrApiCodeGroupLevelTooBig
	ErrApiCodeCapacityTooBig
	ErrApiCodeEmailRepeat
	ErrApiCodeDelTopGroup
	ErrApiCodeAgentMax = errCodeApiServer | errMask
)

func (ErrCode) Code

func (e ErrCode) Code() uint64

func (ErrCode) Error

func (e ErrCode) Error() string

type Error

type Error struct {
	*Status `json:"status,omitempty"`
	Cause   error  `json:"cause,omitempty"`
	Stack   string `json:"-"`
}

func BadRequest

func BadRequest() *Error

BadRequest new 请求错误 that is mapped to a 400 response.

func ErrStatus

func ErrStatus(status interface{}) *Error

ErrStatus api端将rpc的Status转换成 *Error

func New

func New(err error, opts ...StatusFunc) *Error

func NotFound

func NotFound(reason, message string) *Error

NotFound new NotFound error that is mapped to a 404 response.

func Unauthorized

func Unauthorized(reason, message string) *Error

Unauthorized new 未授权错误 error that is mapped to a 401 response.

func (Error) Error

func (e Error) Error() string

func (*Error) GRPCStatus

func (e *Error) GRPCStatus() *status.Status

GRPCStatus returns the Status represented by se.

func (*Error) Is

func (e *Error) Is(err error) bool

Is 传入error 判断是否是自定的 Error 通过As 会将传入的 err 赋值给 new(Error) 的 se。

func (*Error) Unwrap

func (e *Error) Unwrap()

Unwrap 忽略错误直接panic

func (*Error) UnwrapFunc

func (e *Error) UnwrapFunc(f func() interface{})

UnwrapFunc 执行额外错误实现

type ErrorInter

type ErrorInter interface {
	Is(err error) bool
}

type ErrorModels

type ErrorModels []*Status

type Status

type Status struct {
	Code     uint64            `protobuf:"varint,1,opt,name=Code,proto3" json:"Code,omitempty"`
	Reason   string            `protobuf:"bytes,2,opt,name=reason,proto3" json:"reason,omitempty"`
	Message  string            `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"`
	Metadata map[string]string `` /* 157-byte string literal not displayed */
	// contains filtered or unexported fields
}

func (*Status) Descriptor deprecated

func (*Status) Descriptor() ([]byte, []int)

Deprecated: Use Status.ProtoReflect.Descriptor instead.

func (*Status) GetCode

func (x *Status) GetCode() uint64

func (*Status) GetMessage

func (x *Status) GetMessage() string

func (*Status) GetMetadata

func (x *Status) GetMetadata() map[string]string

func (*Status) GetReason

func (x *Status) GetReason() string

func (*Status) ProtoMessage

func (*Status) ProtoMessage()

func (*Status) ProtoReflect

func (x *Status) ProtoReflect() protoreflect.Message

func (*Status) Reset

func (x *Status) Reset()

func (*Status) String

func (x *Status) String() string

func (*Status) StringTo16

func (s *Status) StringTo16() string

type StatusFunc

type StatusFunc func(status *Status)

func WithField added in v1.0.3

func WithField(key, value string) StatusFunc

func WithReason

func WithReason(reason string) StatusFunc

Jump to

Keyboard shortcuts

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