chain: github.com/johnt337/chain/errors Index | Files

package errors

import "github.com/johnt337/chain/errors"

Package errors implements a basic error wrapping pattern, so that errors can be annotated with additional information without losing the original error.

Example:

import "chain/errors"

func query() error {
	err := pq.Exec("SELECT...")
	if err != nil {
		return errors.Wrap(err, "select query failed")
	}

	err = pq.Exec("INSERT...")
	if err != nil {
		return errors.Wrap(err, "insert query failed")
	}

	return nil
}

func main() {
	err := query()
	if _, ok := errors.Root(err).(sql.ErrNoRows); ok {
		log.Println("There were no results")
		return
	} else if err != nil {
		log.Println(err)
		return
	}

	log.Println("success")
}

When to wrap errors

Errors should be wrapped with additional messages when the context is ambiguous. This includes when the error could arise in multiple locations in the same function, when the error is very common and likely to appear at different points in the call tree (e.g., JSON serialization errors), or when you need specific parameters alongside the original error message.

Error handling best practices

Errors are part of a function's interface. If you expect the caller to perform conditional error handling, you should document the errors returned by your function in a function comment, and include it as part of your unit tests.

Be disciplined about validating user input. Programs should draw a very clear distinction between user errors and internal errors.

Avoid redundant error logging. If you return an error, assume it will be logged higher up the call stack. For a given project, choose an appropriate layer to handle error logging.

Index

Package Files

doc.go errors.go stack.go writer.go

func Data Uses

func Data(err error) map[string]interface{}

Data returns the data item in err, if any.

func Detail Uses

func Detail(err error) string

Detail returns the detail message contained in err, if any. An error has a detail message if it was made by WithDetail or WithDetailf.

func New Uses

func New(text string) error

New returns an error that formats as the given text.

func Root Uses

func Root(e error) error

Root returns the original error that was wrapped by one or more calls to Wrap. If e does not wrap other errors, it will be returned as-is.

func WithData Uses

func WithData(err error, keyval ...interface{}) error

WithData returns a new error that wraps err as a chain error message containing a value of type map[string]interface{} as an extra data item. The map contains the values in the map in err, if any, plus the items in keyval. Keyval takes the form

k1, v1, k2, v2, ...

Values kN must be strings. Calling Data on the returned error yields the map. Note that if err already has a data item of any other type, it will not be accessible via the returned error value.

func WithDetail Uses

func WithDetail(err error, text string) error

WithDetail returns a new error that wraps err as a chain error messsage containing text as its additional context. Function Detail will return the given text when called on the new error value.

func WithDetailf Uses

func WithDetailf(err error, format string, v ...interface{}) error

WithDetailf is like WithDetail, except it formats the detail message as in fmt.Printf. Function Detail will return the formatted text when called on the new error value.

func Wrap Uses

func Wrap(err error, a ...interface{}) error

Wrap adds a context message and stack trace to err and returns a new error with the new context. Arguments are handled as in fmt.Print. Use Root to recover the original error wrapped by one or more calls to Wrap. Use Stack to recover the stack trace. Wrap returns nil if err is nil.

func Wrapf Uses

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

Wrapf is like Wrap, but arguments are handled as in fmt.Printf.

type StackFrame Uses

type StackFrame struct {
    Func string
    File string
    Line int
}

StackFrame represents a single entry in a stack trace.

func Stack Uses

func Stack(err error) []StackFrame

Stack returns the stack trace of an error. The error must contain the stack trace, or wrap an error that has a stack trace,

func (StackFrame) String Uses

func (f StackFrame) String() string

String satisfies the fmt.Stringer interface.

type Writer Uses

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

Writer is in an implementation of the "sticky error writer" pattern as described in https://blog.golang.org/errors-are-values.

A Writer makes one call on the underlying writer for each call to Write, until an error is returned. From that point on, it makes no calls on the underlying writer, and returns the same error value every time.

func NewWriter Uses

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w until an error is returned.

func (*Writer) Err Uses

func (w *Writer) Err() error

Err returns the first error encountered by Write, if any.

func (*Writer) Write Uses

func (w *Writer) Write(buf []byte) (n int, err error)

Write makes one call on the underlying writer if no error has previously occurred.

func (*Writer) Written Uses

func (w *Writer) Written() int64

Written returns the number of bytes written to the underlying writer.

Package errors imports 5 packages (graph). Updated 2019-02-15. Refresh now. Tools for package owners.