errors

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: MIT Imports: 10 Imported by: 1

Documentation

Index

Constants

View Source
const (
	UnknownCode   = 500
	UnknownReason = ""
)

Variables

View Source
var (
	// optional int32 code = 51002;
	E_Code = &file_errors_proto_extTypes[1]
)

Extension fields to descriptorpb.EnumValueOptions.

View Source
var (
	// optional int32 default_code = 51001;
	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 any) bool

As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

The tree consists of err itself, followed by the errors obtained by repeatedly calling its Unwrap() error or Unwrap() []error method. When err wraps multiple errors, As examines err followed by a depth-first traversal of its children.

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(any) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

An error type might provide an As method so it can be treated as if it were a different error type.

As panics if target is not a non-nil pointer to either a type that implements error, or to any interface type.

func Code

func Code(err error) int

func Errorf

func Errorf(code int, reason, format string, a ...any) error

func Is

func Is(err, target error) bool

Is reports whether any error in err's tree matches target.

The tree consists of err itself, followed by the errors obtained by repeatedly calling its Unwrap() error or Unwrap() []error method. When err wraps multiple errors, Is examines err followed by a depth-first traversal of its children.

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.

An error type might provide an Is method so it can be treated as equivalent to an existing error. For example, if MyError defines

func (m MyError) Is(target error) bool { return target == fs.ErrExist }

then Is(MyError{}, fs.ErrExist) returns true. See syscall.Errno.Is for an example in the standard library. An Is method should only shallowly compare err and the target and not call Unwrap on either.

func Reason

func Reason(err error) string

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.

Unwrap only calls a method of the form "Unwrap() error". In particular Unwrap does not unwrap errors returned by [Join].

Types

type Error

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

func Clone

func Clone(err *Error) *Error

func FromError

func FromError(err error) *Error

func New

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

func Newf

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

func (*Error) Error

func (e *Error) Error() string

func (*Error) GRPCStatus

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

func (*Error) Is

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

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(metadata map[string]string) *Error

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 `` /* 143-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