werror

package
v0.0.0-...-9a2c5cd Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package werror provides a way to return detailed information for an RPC request error. The error is normally JSON encoded.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aborted

func Aborted(msg string, wrappers ...Wrapper) error

func Append

func Append(err error, msg string, wrappers ...Wrapper) error

Append is a convenience function for the AppendMessage wrapper.

func Appendf

func Appendf(err error, format string, args ...interface{}) error

Appendf is a convenience function for the AppendMessagef wrapper. The args can be format arguments mixed with Wrappers.

func As

func As(err error, target interface{}) 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 Cause

func Cause(err error) error

Cause returns the cause of the argument.

func Code

func Code(err error) codes.Code

Code converts an error to gRPC status code. All errors map to Internal, unless the error has code attached. If err is nil, returns OK.

func Details

func Details(err error) string

Details returns err.Error() and any additional details. The details of each error cause chain will also be printed.

func Forbidden

func Forbidden(msg string, wrappers ...Wrapper) error

func Format

func Format(s fmt.State, verb rune, err error)

Format adapts errors to fmt.Formatter interface. It's intended to be used help error impls implement fmt.Formatter, e.g.:

    func (e *myErr) Format(f fmt.State, verb rune) {
	     Format(f, verb, e)
    }

func ID

func ID(err error) string

func Internal

func Internal(msg string, wrappers ...Wrapper) error

func InvalidArgument

func InvalidArgument(msg string, wrappers ...Wrapper) error

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 Lookup

func Lookup(err error, key interface{}) (interface{}, bool)

Lookup returns the value for the key, and a boolean indicating whether the value was set. Will not search causes.

func New

func New(msg string, wrappers ...Wrapper) error

New creates a new error, with a stack attached. The equivalent of golang's errors.New()

func NotFound

func NotFound(msg string, wrappers ...Wrapper) error

func Prepend

func Prepend(err error, msg string, wrappers ...Wrapper) error

Prepend is a convenience function for the PrependMessage wrapper.

func Prependf

func Prependf(err error, format string, args ...interface{}) error

Prependf is a convenience function for the PrependMessagef wrapper. The args can be format arguments mixed with Wrappers.

func Set

func Set(err error, key, value interface{}) error

Set wraps an error with a key/value pair. This is the simplest form of associating a value with an error. It is mainly intended as a primitive for writing Wrapper implementations.

func Unauthenticated

func Unauthenticated(msg string, wrappers ...Wrapper) error

func Unavailable

func Unavailable(msg string, wrappers ...Wrapper) error

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.

func Value

func Value(err error, key interface{}) interface{}

Value returns the value for key, or nil if not set. Will not search causes.

func Values

func Values(err error) map[interface{}]interface{}

Values returns a map of all values attached to the error If a key has been attached multiple times, the map will contain the last value mapped.

func Wrap

func Wrap(err error, wrappers ...Wrapper) error

Wrap adds context to errors by applying Wrappers. See WithXXX() functions for Wrappers supplied by this package.

Types

type ErrKey

type ErrKey int
const (
	ErrKeyNone ErrKey = iota
	ErrKeyID
	ErrKeyMessage
	ErrKeyCode
)

func (ErrKey) String

func (e ErrKey) String() string

type Wrapper

type Wrapper interface {

	// Wrap returns a new error, wrapping the argument, and typically adding some context information.
	Wrap(err error) error
}

Wrapper knows how to wrap errors with context information.

func AppendMessage

func AppendMessage(msg string) Wrapper

AppendMessage a message after the current error message, in the format "original: new".

func AppendMessagef

func AppendMessagef(format string, args ...interface{}) Wrapper

AppendMessagef is the same as AppendMessage, but with a formatted message.

func PrependMessage

func PrependMessage(msg string) Wrapper

PrependMessage a message before the current error message, in the format "new: original".

func PrependMessagef

func PrependMessagef(format string, args ...interface{}) Wrapper

PrependMessagef is the same as PrependMessage, but with a formatted message.

func WithCause

func WithCause(err error) Wrapper

WithCause sets one error as the cause of another error. This is useful for associating errors from lower API levels with sentinel errors in higher API levels. errors.Is() and errors.As() will traverse both the main chain of error wrappers, and down the chain of causes.

func WithCode

func WithCode(code codes.Code) Wrapper

WithCode associates an gRPC status code with an error.

func WithID

func WithID(msg string) Wrapper

WithID set's error ID (pkg.file.func).

func WithValue

func WithValue(key, value interface{}) Wrapper

WithValue associates a key/value pair with an error.

type WrapperFunc

type WrapperFunc func(error) error

WrapperFunc implements Wrapper.

func (WrapperFunc) Wrap

func (w WrapperFunc) Wrap(err error) error

Wrap implements the Wrapper interface.

Jump to

Keyboard shortcuts

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