cerrors

package
v0.13.0-nightly.20241112 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cerrors contains functions related to error handling.

The standard library's errors package is missing some functionality which we need, such as stack traces. To be certain that all errors created in Conduit are created with the additional information, usage of this package is mandatory.

At present, the package acts as a "thin forwarding layer", where we "mix and match" functions from different packages.

Package cerrors contains functions related to error handling.

The standard library's errors package is missing some functionality which we need, such as stack traces. To be certain that all errors created in Conduit are created with the additional information, usage of this package is mandatory.

At present, the package acts as a "thin forwarding layer", where we "mix and match" functions from different packages.

Index

Constants

This section is empty.

Variables

View Source
var (
	// xerrors is used, since it provides the stack traces too
	New    = xerrors.New    //nolint:forbidigo // xerrors.New is allowed here, but not anywhere else
	Errorf = xerrors.Errorf //nolint:forbidigo // xerrors.Errorf is allowed here, but not anywhere else
	Is     = errors.Is
	As     = errors.As
	Unwrap = errors.Unwrap
	Join   = errors.Join
)
View Source
var (
	// ErrNotImpl should be used when a functionality which is not yet implemented was called.
	ErrNotImpl = New("not impl")
	// ErrEmptyID should be used when an entity was requested, but the ID was not provided.
	ErrEmptyID = New("empty ID")
)

Functions

func FatalError added in v0.12.0

func FatalError(err error) error

FatalError creates a new fatalError.

func ForEach added in v0.11.0

func ForEach(err error, fn func(error))

ForEach is a utility function that can be used to iterate over all errors in a multierror created using Join. It will call the provided function for each error in the chain.

func GetStackTrace

func GetStackTrace(err error) interface{}

func IsFatalError added in v0.12.0

func IsFatalError(err error) bool

IsFatalError checks if the error is a fatalError.

func LogOrReplace added in v0.3.0

func LogOrReplace(oldErr, newErr error, log func()) error

LogOrReplace is a utility meant to be called in deferred functions that can produce an error. In that case we have two options:

  1. Group errors together (e.g. with the multierror package).
  2. Use LogOrReplace to either log the error or return it.

The second option is preferable when the original error returning from the function is more important than the deferred error. LogOrReplace will return the original error if it is not nil, otherwise it will return the new error. If both errors are not nil, then the log function will be called, that can be used to log the new error.

Example how it's supposed to be used:

func() (err error) {
  defer func() {
    cleanupErr := cleanup()
    err = cerrors.LogOrReplace(err, cleanupErr, func() {
      fmt.Printf("cleanup error: %v", cleanupErr)
    })
  }
  // execute logic that can produce an error
  return err
}

Types

type Frame

type Frame struct {
	Func string `json:"func,omitempty"`
	File string `json:"file,omitempty"`
	Line int    `json:"line,omitempty"`
}

Jump to

Keyboard shortcuts

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