errors

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: Apache-2.0 Imports: 8 Imported by: 8,237

Documentation

Overview

Package errors implements custom error interfaces for cosmos-sdk.

Error declarations should be generic and cover broad range of cases. Each returned error instance can wrap a generic error declaration to provide more details.

This package provides a broad range of errors declared that fits all common cases. If an error is very specific for an extension it can be registered outside of the errors package. If it will be needed my many extensions, please consider registering it in the errors package. To create a new error instance use Register function. You must provide a unique, non zero error code and a short description, for example:

var ErrZeroDivision = errors.Register(9241, "zero division")

When returning an error, you can attach to it an additional context information by using Wrap function, for example:

   func safeDiv(val, div int) (int, err) {
	   if div == 0 {
		   return 0, errors.Wrapf(ErrZeroDivision, "cannot divide %d", val)
	   }
	   return val / div, nil
   }

The first time an error instance is wrapped a stacktrace is attached as well. Stacktrace information can be printed using %+v and %v formats.

%s  is just the error message
%+v is the full stack trace
%v  appends a compressed [filename:line] where the error was created

Index

Constants

View Source
const (
	// SuccessABCICode declares an ABCI response use 0 to signal that the
	// processing was successful and no error is returned.
	SuccessABCICode uint32 = 0
)
View Source
const UndefinedCodespace = "undefined"

UndefinedCodespace when we explicitly declare no codespace

Variables

View Source
var (

	// ErrStopIterating is used to break out of an iteration
	ErrStopIterating = Register(UndefinedCodespace, 2, "stop iterating")

	// ErrPanic should only be set when we recovering from a panic
	ErrPanic = Register(UndefinedCodespace, 111222, "panic")
)

Functions

func ABCIError

func ABCIError(codespace string, code uint32, log string) error

ABCIError will resolve an error code/log from an abci result into an error message. If the code is registered, it will map it back to the canonical error, so we can do eg. ErrNotFound.Is(err) on something we get back from an external API.

This should *only* be used in clients, not in the server side. The server (abci app / blockchain) should only refer to registered errors

func ABCIInfo

func ABCIInfo(err error, debug bool) (codespace string, code uint32, log string)

ABCIInfo returns the ABCI error information as consumed by the tendermint client. Returned codespace, code, and log message should be used as a ABCI response. Any error that does not provide ABCICode information is categorized as error with code 1, codespace UndefinedCodespace When not running in a debug mode all messages of errors that do not provide ABCICode information are replaced with generic "internal error". Errors without an ABCICode information as considered internal.

func AssertNil

func AssertNil(err error)

AssertNil panics on error Should be only used with interface methods, which require return error, but the error is always nil

func IsOf

func IsOf(received error, targets ...error) bool

IsOf checks if a received error is caused by one of the target errors. It extends the errors.Is functionality to a list of errors.

func Recover

func Recover(err *error)

Recover captures a panic and stop its propagation. If panic happens it is transformed into a ErrPanic instance and assigned to given error. Call this function using defer in order to work as expected.

func WithType

func WithType(err error, obj interface{}) error

WithType is a helper to augment an error with a corresponding type message

func Wrap

func Wrap(err error, description string) error

Wrap extends given error with an additional information.

If the wrapped error does not provide ABCICode method (ie. stdlib errors), it will be labeled as internal error.

If err is nil, this returns nil, avoiding the need for an if statement when wrapping a error returned at the end of a function

func Wrapf

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

Wrapf extends given error with an additional information.

This function works like Wrap function with additional functionality of formatting the input as specified.

Types

type Error

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

Error represents a root error.

Weave framework is using root error to categorize issues. Each instance created during the runtime should wrap one of the declared root errors. This allows error tests and returning all errors to the client in a safe manner.

All popular root errors are declared in this package. If an extension has to declare a custom root error, always use Register function to ensure error code uniqueness.

func New

func New(codespace string, code uint32, desc string) *Error

New is an alias for Register.

func Register

func Register(codespace string, code uint32, description string) *Error

Register returns an error instance that should be used as the base for creating error instances during runtime.

Popular root errors are declared in this package, but extensions may want to declare custom codes. This function ensures that no error code is used twice. Attempt to reuse an error code results in panic.

Use this function only during a program startup phase.

func RegisterWithGRPCCode

func RegisterWithGRPCCode(codespace string, code uint32, grpcCode grpccodes.Code, description string) *Error

RegisterWithGRPCCode is a version of Register that associates a gRPC error code with a registered error.

func (Error) ABCICode

func (e Error) ABCICode() uint32

func (Error) Codespace

func (e Error) Codespace() string

func (Error) Error

func (e Error) Error() string

func (*Error) GRPCStatus

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

func (*Error) Is

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

Is check if given error instance is of a given kind/type. This involves unwrapping given error using the Cause method if available.

func (*Error) Wrap

func (e *Error) Wrap(desc string) error

Wrap extends this error with an additional information. It's a handy function to call Wrap with sdk errors.

func (*Error) Wrapf

func (e *Error) Wrapf(desc string, args ...interface{}) error

Wrapf extends this error with an additional information. It's a handy function to call Wrapf with sdk errors.

Jump to

Keyboard shortcuts

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