errorx

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

nolint:gomnd

Index

Constants

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

Variables

This section is empty.

Functions

func As

func As(err error, target any) 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 AsLast

func AsLast(err error, target any) bool

AsLast finds the last error in err's chain that matches target, and if so, sets target to that error value and returns true.

func Code

func Code(err error) int

Code returns the http code for an 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 FromGRPCCode

func FromGRPCCode(code codes.Code) int

FromGRPCCode converts a gRPC error code into the corresponding HTTP response status. See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto

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 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 OK

func OK() *proto_status.Status

func Reason

func Reason(err error) string

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

func ToGRPCCode

func ToGRPCCode(code int) codes.Code

ToGRPCCode converts an HTTP error code into the corresponding gRPC response status. See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto

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 {
	proto_status.Status
	// contains filtered or unexported fields
}

Error is a status error.

func Clone

func Clone(err *Error) *Error

Clone deep clone error to a new error.

func ErrorBadRequest

func ErrorBadRequest(reason, message string) *Error

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

func ErrorClientClosed

func ErrorClientClosed(reason, message string) *Error

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

func ErrorConflict

func ErrorConflict(reason, message string) *Error

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

func ErrorForbidden

func ErrorForbidden(reason, message string) *Error

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

func ErrorGatewayTimeout

func ErrorGatewayTimeout(reason, message string) *Error

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

func ErrorInternalServer

func ErrorInternalServer(reason, message string) *Error

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

func ErrorNotFound

func ErrorNotFound(reason, message string) *Error

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

func ErrorServiceUnavailable

func ErrorServiceUnavailable(reason, message string) *Error

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

func ErrorUnauthorized

func ErrorUnauthorized(reason, message string) *Error

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

func ErrorfBadRequest

func ErrorfBadRequest(reason, format string, a ...interface{}) *Error

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

func ErrorfClientClosed

func ErrorfClientClosed(reason, format string, a ...interface{}) *Error

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

func ErrorfConflict

func ErrorfConflict(reason, format string, a ...interface{}) *Error

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

func ErrorfForbidden

func ErrorfForbidden(reason, format string, a ...interface{}) *Error

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

func ErrorfGatewayTimeout

func ErrorfGatewayTimeout(reason, format string, a ...interface{}) *Error

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

func ErrorfInternalServer

func ErrorfInternalServer(reason, format string, a ...interface{}) *Error

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

func ErrorfNotFound

func ErrorfNotFound(reason, format string, a ...interface{}) *Error

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

func ErrorfServiceUnavailable

func ErrorfServiceUnavailable(reason, format string, a ...interface{}) *Error

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

func ErrorfUnauthorized

func ErrorfUnauthorized(reason, format string, a ...interface{}) *Error

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

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, 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 (*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 matches each error in the chain with the target value.

func (*Error) Proto

func (e *Error) Proto() *proto_status.Status

Proto returns s's status as an spb.Status proto message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap provides compatibility for Go 1.13 error chains.

func (*Error) WithCause

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

WithCause with the underlying cause of the error.

func (*Error) WithMetadata

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
	Reason   string
	Message  string
	Metadata map[string]string
}

Jump to

Keyboard shortcuts

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