ecode

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 11 Imported by: 2

README

ecode

error code handle

Documentation

Index

Constants

View Source
const (
	// UnknownCode is unknown code for error info.
	UnknownCode = http.StatusInternalServerError
	// UnknownReason is unknown reason for error info.
	UnknownReason = ""
	// ClientClosed is non-standard http status code,
	// which defined by nginx.
	// https://httpstatus.in/499/
	ClientClosed = 499
)

Variables

View Source
var (
	// base error
	Success               = New(http.StatusOK, "SUCCESS", "success")
	RequestErr            = New(http.StatusBadRequest, "PARAM_ERROR", "request param error")
	UnauthorizedErr       = New(http.StatusUnauthorized, "SIGN_ERROR", "sign error")
	ForbiddenErr          = New(http.StatusForbidden, "NO_AUTH", "no auth")
	NotFoundErr           = New(http.StatusNotFound, "RESOURCE_NOT_FOUND", "resource not found")
	TooManyRequestErr     = New(http.StatusTooManyRequests, "RATELIMIT_EXCEEDED", "ratelimit exceeded")
	ServerErr             = New(http.StatusInternalServerError, "SERVER_ERROR", "server error")
	BadGatewayErr         = New(http.StatusBadGateway, "SERVICE_UNAVAILABLE", "service offline, unavailable")
	ServiceUnavailableErr = New(http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "service protected, unavailable")
)
View Source
var DefaultConverter = statusConverter{}

DefaultConverter default converter.

View Source
var File_errors_proto protoreflect.FileDescriptor

Functions

This section is empty.

Types

type Error

type Error struct {
	Status
	// contains filtered or unexported fields
}

Error struct

func AnalyseError

func AnalyseError(err error) (e2 *Error)

AnalyseError analyse error info

func DeepClone

func DeepClone(err *Error) *Error

DeepClone deep clone error to a new error.

func FromError

func FromError(err error) *Error

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

func New

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

New returns an error object for the code, message.

func (*Error) Code

func (e *Error) Code() int

Code returns the code of the error.

func (*Error) Equal added in v0.0.2

func (e *Error) Equal(code int, reason ...string) bool

Equal matches error from code and reason.

func (*Error) Error

func (e *Error) Error() string

func (*Error) GRPCStatus

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

GRPCStatus returns the Status represented by error.

func (*Error) Is added in v0.0.2

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

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

func (*Error) Message

func (e *Error) Message() string

Message returns the message of the error.

func (*Error) Reason added in v0.0.2

func (e *Error) Reason() string

Reason returns the reason of the error.

func (*Error) Unwrap added in v0.0.2

func (e *Error) Unwrap() error

Unwrap provides compatibility for Go 1.13 error chains.

func (*Error) WithCause added in v0.0.2

func (e *Error) WithCause(cause error) *Error

WithCause with the underlying cause of the error.

func (*Error) WithMetadata added in v0.0.2

func (e *Error) WithMetadata(md map[string]string) *Error

WithMetadata with an MD formed by the mapping of key, value.

type Status

type Status 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 (*Status) Descriptor deprecated

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

Deprecated: Use Status.ProtoReflect.Descriptor instead.

func (*Status) GetCode

func (x *Status) GetCode() int32

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

Jump to

Keyboard shortcuts

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