errors

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: MIT Imports: 14 Imported by: 6

README

errors

从 Kratos 项目借鉴来的错误处理包。

Documentation

Index

Constants

View Source
const (
	// UnknownReason is unknown reason for error info.
	UnknownReason = "UNKNOWN"
	// SupportPackageIsVersion1 is used for generation validating,
	// and this constant should not be referenced by any other code.
	SupportPackageIsVersion1 = true
	// ClientClosedHttpCode is non-standard http status code,
	// which defined by nginx.
	// https://httpstatus.in/499/
	ClientClosedHttpCode = 499
)

Variables

View Source
var (
	// optional bool error_define = 1108;
	E_ErrorDefine = &file_yimi_errors_v1_errors_proto_extTypes[0]
	// optional google.rpc.Code default_code = 1109;
	E_DefaultCode = &file_yimi_errors_v1_errors_proto_extTypes[1]
)

Extension fields to descriptorpb.EnumOptions.

View Source
var (
	// optional google.rpc.Code code = 1110;
	E_Code = &file_yimi_errors_v1_errors_proto_extTypes[2]
	// optional int32 force_http_code = 1111;
	E_ForceHttpCode = &file_yimi_errors_v1_errors_proto_extTypes[3]
)

Extension fields to descriptorpb.EnumValueOptions.

View Source
var File_yimi_errors_v1_errors_proto protoreflect.FileDescriptor

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 Code

func Code(err error) codes.Code

Code returns the code for an error. It supports wrapped errors.

func Errorf

func Errorf(code codes.Code, reason, format string, a ...any) 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 IsAborted

func IsAborted(err error) bool

IsAborted checks if an error is an Error with codes.Aborted which mapping to HTTP code 409. It supports wrapped errors.

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists checks if an error is an Error with codes.AlreadyExists which mapping to HTTP code 409. It supports wrapped errors.

func IsCanceled

func IsCanceled(err error) bool

IsCanceled checks if an error is an Error with codes.Canceled which mapping to HTTP code 499. This function supports wrapped errors.

func IsDataLoss

func IsDataLoss(err error) bool

IsDataLoss checks if an error is an Error with codes.DataLoss which mapping to HTTP code 500. It supports wrapped errors.

func IsDeadlineExceeded

func IsDeadlineExceeded(err error) bool

IsDeadlineExceeded checks if an error is an Error with codes.DeadlineExceeded which mapping to HTTP code 504. It supports wrapped errors.

func IsFailedPrecondition

func IsFailedPrecondition(err error) bool

IsFailedPrecondition checks if an error is an Error with codes.FailedPrecondition which mapping to HTTP code 400. It supports wrapped errors.

func IsInternal

func IsInternal(err error) bool

IsInternal checks if an error is an Error with codes.Internal which mapping to HTTP code 500. It supports wrapped errors.

func IsInvalidArgument

func IsInvalidArgument(err error) bool

IsInvalidArgument checks if an error is an Error with codes.InvalidArgument which mapping to HTTP code 400. It supports wrapped errors.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error is an Error with codes.NotFound which mapping to HTTP code 404. It supports wrapped errors.

func IsOutOfRange

func IsOutOfRange(err error) bool

IsOutOfRange checks if an error is an Error with codes.OutOfRange which mapping to HTTP code 400. It supports wrapped errors.

func IsPermissionDenied

func IsPermissionDenied(err error) bool

IsPermissionDenied checks if an error is an Error with codes.PermissionDenied which mapping to HTTP code 403. It supports wrapped errors.

func IsResourceExhausted

func IsResourceExhausted(err error) bool

IsResourceExhausted checks if an error is an Error with codes.ResourceExhausted which mapping to HTTP code 429. It supports wrapped errors.

func IsUnauthenticated

func IsUnauthenticated(err error) bool

IsUnauthenticated checks if an error is an Error with codes.Unauthenticated which mapping to HTTP code 401. It supports wrapped errors.

func IsUnavailable

func IsUnavailable(err error) bool

IsUnavailable checks if an error is an Error with codes.Unavailable which mapping to HTTP code 503. It supports wrapped errors.

func IsUnimplemented

func IsUnimplemented(err error) bool

IsUnimplemented checks if an error is an Error with codes.Unimplemented which mapping to HTTP code 501. It supports wrapped errors.

func IsUnknown

func IsUnknown(err error) bool

IsUnknown checks if an error is an Error with codes.Unknown which mapping to HTTP code 500. It supports wrapped errors.

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

Error is an error with Status and an optional cause.

func Aborted

func Aborted(reason, message string) *Error

Aborted creates an Error with codes.Aborted which mapping to HTTP code 409.

func AlreadyExists

func AlreadyExists(reason, message string) *Error

AlreadyExists creates an Error with codes.AlreadyExists which mapping to HTTP code 409.

func Canceled

func Canceled(reason, message string) *Error

Canceled creates an Error with codes.Canceled which mapping to HTTP code 499.

func Clone

func Clone(err *Error) *Error

Clone deep clones Error to a new Error.

func DataLoss

func DataLoss(reason, message string) *Error

DataLoss creates an Error with codes.DataLoss which mapping to HTTP code 500.

func DeadlineExceeded

func DeadlineExceeded(reason, message string) *Error

DeadlineExceeded creates an Error with codes.DeadlineExceeded which mapping to HTTP code 504.

func FailedPrecondition

func FailedPrecondition(reason, message string) *Error

FailedPrecondition creates an Error with codes.FailedPrecondition which mapping to HTTP code 400.

func FromError

func FromError(err error) *Error

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

func Internal

func Internal(reason, message string) *Error

Internal creates an Error with codes.Internal which mapping to HTTP code 500.

func InvalidArgument

func InvalidArgument(reason, message string) *Error

InvalidArgument creates an Error with codes.InvalidArgument which mapping to HTTP code 400.

func New

func New(code codes.Code, reason, message string) *Error

New returns an Error for the code, reason and message.

func Newf

func Newf(code codes.Code, reason, format string, a ...any) *Error

Newf is same as New(code, reason, fmt.Sprintf(format, a...))

func NotFound

func NotFound(reason, message string) *Error

NotFound creates an Error with codes.NotFound which mapping to HTTP code 404.

func OutOfRange

func OutOfRange(reason, message string) *Error

OutOfRange creates an Error with codes.OutOfRange which mapping to HTTP code 400.

func PermissionDenied

func PermissionDenied(reason, message string) *Error

PermissionDenied creates an Error with codes.PermissionDenied which mapping to HTTP code 403.

func ResourceExhausted

func ResourceExhausted(reason, message string) *Error

ResourceExhausted creates an Error with codes.ResourceExhausted which mapping to HTTP code 429.

func Unauthenticated

func Unauthenticated(reason, message string) *Error

Unauthenticated creates an Error with codes.Unauthenticated which mapping to HTTP code 401.

func Unavailable

func Unavailable(reason, message string) *Error

Unavailable creates an Error with codes.Unavailable which mapping to HTTP code 503.

func Unimplemented

func Unimplemented(reason, message string) *Error

Unimplemented creates an Error with codes.Unimplemented which mapping to HTTP code 501.

func Unknown

func Unknown(reason, message string) *Error

Unknown creates an Error with codes.Unknown which mapping to HTTP code 500.

func (*Error) AppendMetadata

func (e *Error) AppendMetadata(key, value string) *Error

AppendMetadata returns a new Error, with a new metadata key-value pair appended or replaced.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

func (*Error) GRPCStatus

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

GRPCStatus returns the Status represented by the Error.

func (*Error) HttpCode

func (e *Error) HttpCode() int32

func (*Error) Is

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

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

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

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 returns a new Error with the underlying cause.

func (*Error) WithForceHttpCode

func (e *Error) WithForceHttpCode(code int32) *Error

WithForceHttpCode returns a new Error that has set an HTTP status code the Error would be mapped to.

func (*Error) WithMetadata

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

WithMetadata returns a new Error with a new key-value metadata map. The metadata of origin Error would be dropped.

type Status

type Status struct {
	Code          code.Code         `protobuf:"varint,1,opt,name=code,proto3,enum=google.rpc.Code" 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 */
	ForceHttpCode int32             `protobuf:"varint,5,opt,name=force_http_code,json=forceHttpCode,proto3" json:"force_http_code,omitempty"`
	// 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() code.Code

func (*Status) GetForceHttpCode

func (x *Status) GetForceHttpCode() 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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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