errorx

package
v0.0.0-...-fc59d6d Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

nolint:gomnd

Index

Constants

View Source
const (
	DefaultStatusCode = 400
	UnknownStatusCode = 500
)
View Source
const (
	ErrCodeUnknown           int32 = -1
	ErrCodeInvalidReqSys     int32 = 40000
	ErrCodeInvalidParamSys   int32 = 40001
	ErrCodeInvalidHeaderSys  int32 = 40002
	ErrCodeInvalidBodySys    int32 = 40003
	ErrCodeRecordNotFoundSys int32 = 40004
)
View Source
const (
	// StatusClientClosed is non-standard http status code,
	// which defined by nginx.
	// https://httpstatus.in/499/
	StatusClientClosed = 499
)

Variables

View Source
var File_errorx_proto protoreflect.FileDescriptor

Functions

func ErrCode

func ErrCode(err error) int32

ErrCode returns the reason for a particular error. It supports wrapped errorx.

func ErrorIs

func ErrorIs(err error, errCode int32) bool

ErrorIs returns true if err is an *Error and its ErrorCode matches errCode.

func Errorf

func Errorf(statusCode int32, errCode int32, format string, a ...interface{}) error

func FromGRPCCode

func FromGRPCCode(code codes.Code) int

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

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 IsClientClosed 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 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 IsServiceUnavailable

func IsServiceUnavailable(err error) bool

IsServiceUnavailable determines if err is an error which indicates an Unavailable 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 RegisterError

func RegisterError(codeMap map[int32]*ErrCodeDetail)

func SetLang

func SetLang(l string)

func StatusCode

func StatusCode(err error) int

StatusCode returns the http code for an error. It supports wrapped errorx.

func ToGRPCCode

func ToGRPCCode(code int) codes.Code

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

Types

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{}

DefaultConverter default converter.

type ErrCodeDetail

type ErrCodeDetail struct {
	StatusCode int32
	ErrorCode  int32
	Message    string
	Polyglot   map[string]string
}

type Error

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

Error is a status error.

func BadRequest

func BadRequest(message string) *Error

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

func ClientClosed

func ClientClosed(message string) *Error

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

func Clone

func Clone(err *Error) *Error

Clone deep clone error to a new error.

func Conflict

func Conflict(message string) *Error

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

func CreateError

func CreateError(statusCode int32, errCode int32, message string) *Error

CreateError returns an error object for the status code, error code, message.

func CreateErrorf

func CreateErrorf(statusCode int32, errCode int32, format string, a ...interface{}) *Error

func Forbidden

func Forbidden(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.

func GatewayTimeout

func GatewayTimeout(message string) *Error

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

func InternalServer

func InternalServer(message string) *Error

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

func New

func New(errCode int32) *Error

func NewWithMsg

func NewWithMsg(errCode int32, msg string) *Error

NewWithMsg new error from errcode and message

func NewWithStatus

func NewWithStatus(statusCode int32, errCode int32) *Error

NewWithStatus new error from errcode and message

func NotFound

func NotFound(message string) *Error

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

func ServiceUnavailable

func ServiceUnavailable(message string) *Error

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

func Unauthorized

func Unauthorized(message string) *Error

Unauthorized new Unauthorized 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

func (*Error) GetErrorCode

func (e *Error) GetErrorCode() int32

func (*Error) GetStatusCode

func (e *Error) GetStatusCode() int32

func (*Error) Is

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

func (*Error) IsErrCode

func (e *Error) IsErrCode(errCode int32) bool

func (*Error) JSONBytes

func (e *Error) JSONBytes() []byte

JSONBytes returns the JSON representation of the error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) WithCause

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

func (*Error) WithMetadata

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

type ErrorInfo

type ErrorInfo struct {
	StatusCode int32             `protobuf:"varint,1,opt,name=status_code,json=statusCode,proto3" json:"status_code,omitempty"`
	ErrorCode  int32             `protobuf:"varint,2,opt,name=error_code,json=errorCode,proto3" json:"error_code,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 (*ErrorInfo) Descriptor deprecated

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

Deprecated: Use ErrorInfo.ProtoReflect.Descriptor instead.

func (*ErrorInfo) GetErrorCode

func (x *ErrorInfo) GetErrorCode() int32

func (*ErrorInfo) GetMessage

func (x *ErrorInfo) GetMessage() string

func (*ErrorInfo) GetMetadata

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

func (*ErrorInfo) GetStatusCode

func (x *ErrorInfo) GetStatusCode() int32

func (*ErrorInfo) ProtoMessage

func (*ErrorInfo) ProtoMessage()

func (*ErrorInfo) ProtoReflect

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

func (*ErrorInfo) Reset

func (x *ErrorInfo) Reset()

func (*ErrorInfo) String

func (x *ErrorInfo) String() string

Jump to

Keyboard shortcuts

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