errno

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

README

errno

业务域错误可以统一通过 proto 定义业务原因,然后通过 protoc 生成枚举定义和相关 helper functions。

在 errno 包中,错误模型主要跟 http 状态码一致,并且实现相关接口将 Error 与 GRPCStatus 互转。

errno 中的 Error 本身不携带堆栈信息,如果想要在 error 中携带堆栈信息,可以在业务代码中借助 github.com/pkg/errors 实现。 被 errors 库包装过的 Error 依然可以使用内置的 errno.IsXXX 等大错误类型判方法。

依赖项

安装新版 protoc-gen-go ,protoc-gen-go-grpc

Documentation

Index

Constants

View Source
const (
	// UnknownCode is unknown code for error info.
	UnknownCode = 500
	// UnknownReason is unknown reason for error info.
	UnknownReason = ""
	// SupportPackageIsVersion1 this constant should not be referenced by any other code.
	SupportPackageIsVersion1 = true
)
View Source
const (
	DefaultReasonDB             = "DB_ERROR"
	DefaultReasonRedis          = "REDIS_ERROR"
	DefaultReasonRPC            = "RPC_ERROR"
	DefaultReasonUnknown        = "INTERNAL_ERROR"
	DefaultReasonThirdParty     = "THIRD_PARTY_ERROR"
	DefaultReasonParamWrong     = "INVALID_PARAM"
	DefaultReasonSessionExpired = "SESSION_EXPIRED"
	DefaultReasonExisted        = "ALREADY_EXISTED"

	DefaultMessageDB             = "db error"
	DefaultMessageRedis          = "redis error"
	DefaultMessageRPC            = "internal rpc service error"
	DefaultMessageUnknown        = "internal error"
	DefaultMessageThirdParty     = "call third_party api failed"
	DefaultMessageParamWrong     = "invalid param"
	DefaultMessageSessionExpired = "session expired"
	DefaultMessageExisted        = "resource is already existed"
)
View Source
const (
	DefaultReasonBadRequest         = "INVALID_PARAM"
	DefaultReasonUnauthorized       = "UNAUTHORIZED"
	DefaultReasonForbidden          = "FORBIDDEN"
	DefaultReasonNotFound           = "NOT_FOUND"
	DefaultReasonConflict           = "CONFLICT"
	DefaultReasonTooManyRequests    = "TOO_MANY_REQUESTS"
	DefaultReasonInternalServer     = "INTERNAL_SERVER"
	DefaultReasonNotImplemented     = "NOT_IMPLEMENTED"
	DefaultReasonServiceUnavailable = "SERVICE_UNAVAILABLE"
	DefaultReasonGatewayTimeout     = "GATEWAY_TIMEOUT"
	DefaultReasonClientClosed       = "CLIENT_CLOSED"

	DefaultMessageBadRequest         = "invalid param"
	DefaultMessageUnauthorized       = "unauthorized"
	DefaultMessageForbidden          = "forbidden"
	DefaultMessageNotFound           = "not found"
	DefaultMessageConflict           = "conflict"
	DefaultMessageTooManyRequests    = "too many requests"
	DefaultMessageInternalServer     = "internal server"
	DefaultMessageNotImplemented     = "not implemented"
	DefaultMessageServiceUnavailable = "service unavailable"
	DefaultMessageGateWayTimeout     = "gateway timeout"
	DefaultMessageClientClosed       = "client closed"
)

Variables

View Source
var (
	// optional int32 code = 100001;
	E_Code = &file_errors_proto_extTypes[1]
	// optional string message = 100002;
	E_Message = &file_errors_proto_extTypes[2]
)

Extension fields to descriptorpb.EnumValueOptions.

View Source
var (
	// optional int32 default_code = 100001;
	E_DefaultCode = &file_errors_proto_extTypes[0]
)

Extension fields to descriptorpb.EnumOptions.

View Source
var File_errors_proto protoreflect.FileDescriptor

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 returns the http code for a particular error. It supports wrapped errors.

func Errorf

func Errorf(code int, reason, format string, a ...interface{}) error

Errorf returns an error object for the code, message and error info.

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 IsBadRequest

func IsBadRequest(err error) bool

IsBadRequest determines if err is an error which indicates a BadRequest error. It supports wrapped errors.

func IsClientClosed

func IsClientClosed(err error) bool

IsClientClosed determines if err is an error which indicates a ClientClosed error. It supports wrapped errors.

func IsConflict

func IsConflict(err error) bool

IsConflict determines if err is an error which indicates a Conflict error. It supports wrapped errors.

func IsDB

func IsDB(e error) bool

func IsExisted

func IsExisted(e error) bool

func IsForbidden

func IsForbidden(err error) bool

IsForbidden determines if err is an error which indicates a Forbidden error. It supports wrapped errors.

func IsGatewayTimeout

func IsGatewayTimeout(err error) bool

IsGatewayTimeout determines if err is an error which indicates a GatewayTimeout error. It supports wrapped errors.

func IsInternalServer

func IsInternalServer(err error) bool

IsInternalServer determines if err is an error which indicates an Internal error. It supports wrapped errors.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound determines if err is an error which indicates an NotFound error. It supports wrapped errors.

func IsNotImplemented

func IsNotImplemented(err error) bool

IsNotImplemented determines if err is an error which indicates an NotImplemented error. It supports wrapped errors.

func IsParamWrong

func IsParamWrong(e error) bool

func IsRPC

func IsRPC(e error) bool

func IsRedis

func IsRedis(e error) bool

func IsServiceUnavailable

func IsServiceUnavailable(err error) bool

IsServiceUnavailable determines if err is an error which indicates an Unavailable error. It supports wrapped errors.

func IsSessionExpired

func IsSessionExpired(e error) bool

func IsThirdParty

func IsThirdParty(e error) bool

func IsTooManyRequests

func IsTooManyRequests(err error) bool

IsTooManyRequests determines if err is an error which indicates a TooManyRequests error. It supports wrapped errors.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized determines if err is an error which indicates an Unauthorized error. It supports wrapped errors.

func IsUnknown

func IsUnknown(e error) bool

func Reason

func Reason(err error) string

Reason returns the reason for a particular error. It supports wrapped errors.

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 Error

type Error struct {
	Code     int32             `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 BadRequest

func BadRequest(reason, message string) *Error

BadRequest new BadRequest error that is mapped to a 400 response.

func ClientClosed

func ClientClosed(reason, message string) *Error

ClientClosed new ClientClosed error that is mapped to an HTTP 499 response.

func Conflict

func Conflict(reason, message string) *Error

Conflict new Conflict error that is mapped to a 409 response.

func DB

func DB(e ...error) *Error

func Existed

func Existed(e ...error) *Error

func Forbidden

func Forbidden(reason, message string) *Error

Forbidden new Forbidden error that is mapped to a 403 response.

func FromError

func FromError(err error) *Error

FromError try to convert an error to *Error. It supports wrapped errors.

func GatewayTimeout

func GatewayTimeout(reason, message string) *Error

GatewayTimeout new GatewayTimeout error that is mapped to an HTTP 504 response.

func InternalServer

func InternalServer(reason, message string) *Error

InternalServer new InternalServer error that is mapped to a 500 response.

func New

func New(code int, reason, message string) *Error

New returns an error object for the code, message.

func Newf

func Newf(code int, reason, format string, a ...interface{}) *Error

Newf New(code fmt.Sprintf(format, a...))

func NotFound

func NotFound(reason, message string) *Error

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

func NotImplemented

func NotImplemented(reason, message string) *Error

NotImplemented new NotImplemented error that is mapped to a 500 response.

func ParamWrong

func ParamWrong(e ...error) *Error

func RPC

func RPC(e ...error) *Error

func Redis

func Redis(e ...error) *Error

func ServiceUnavailable

func ServiceUnavailable(reason, message string) *Error

ServiceUnavailable new ServiceUnavailable error that is mapped to an HTTP 503 response.

func SessionExpired

func SessionExpired(e ...error) *Error

func ThirdParty

func ThirdParty(e ...error) *Error

func TooManyRequests

func TooManyRequests(reason, message string) *Error

TooManyRequests new TooManyRequests error that is mapped to an HTTP 429 response.

func Unauthorized

func Unauthorized(reason, message string) *Error

Unauthorized new Unauthorized error that is mapped to a 401 response.

func Unknown

func Unknown(e ...error) *Error

func (*Error) CopyWithCauseError

func (x *Error) CopyWithCauseError(e error) *Error

CopyWithCauseError copies the error instance and returns a new one with given error.

func (*Error) CopyWithMessage

func (x *Error) CopyWithMessage(msg string) *Error

CopyWithMessage copies the error instance and returns a new one with given message.

func (*Error) CopyWithMetadata

func (x *Error) CopyWithMetadata(md map[string]string) *Error

CopyWithMetadata copies the error instance and returns a new one with an MD formed by the mapping of key, value.

func (*Error) Descriptor deprecated

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

Deprecated: Use Error.ProtoReflect.Descriptor instead.

func (*Error) Error

func (x *Error) Error() string

func (*Error) GRPCStatus

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

GRPCStatus returns the Status represented by se.

func (*Error) GetCode

func (x *Error) GetCode() int32

func (*Error) GetMessage

func (x *Error) GetMessage() string

func (*Error) GetMetadata

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

func (*Error) GetReason

func (x *Error) GetReason() string

func (*Error) Is

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

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

func (*Error) ProtoMessage

func (*Error) ProtoMessage()

func (*Error) ProtoReflect

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

func (*Error) Reset

func (x *Error) Reset()

func (*Error) String

func (x *Error) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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