errors

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2020 License: Apache-2.0 Imports: 7 Imported by: 32

README

errors

CircleCI

errors provides utility routines for working with errors that are compatible with go 1.13+ and for annotating errors with location and other information.

It currently provides:

  1. errors.M which can be used to store multiple error values. errors.M is thread safe.
errs := errors.M{}
...
errs.Append(fn(a))
...
errs.Append(fn(b))
...
err := errs.Err()
  1. errors.Caller, errors.Annotate and errors.AnnotateAll which can be used to annotate an existing error with location information on the caller or an arbitrary string respectively.
err := errors.Caller(os.ErrNotExist)
fmt.Printf("%v\n", err)
fmt.Printf("%v\n", errors.Unwrap(err))
// Output:
// errors/caller_test.go:17: file does not exist
// file does not exist

Documentation

Overview

Package errors provides utility routines for working with errors that are compatible with go 1.13+ and for annotating errors with location and other information. It provides errors.M which can be used to collect and work with multiple errors in a thread safe manner. It also provides convenience routines for annotating existing errors with caller and other information.

errs := errors.M{}
errs.Append(fn(a))
errs.Append(fn(b))
err := errs.Err()

Annotations, can be added as follows:

err := errors.Caller(os.ErrNotExist)

Where:

fmt.Printf("%v\n", err)
fmt.Printf("%v\n", errors.Unwrap(err))

Would produce:

errors/caller_test.go:17: file does not exist
file does not exist

Annotated errors can be passed to errors.M:

errs := errors.M{}
errs.Append(errors.Caller(fn(a)))
errs.Append(errors.Caller(fn(b)))
err := errs.Err()

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Annotate added in v0.0.3

func Annotate(annotation string, err error) error

Annotate returns an error representing the original error and the supplied annotation.

func AnnotateAll added in v0.0.3

func AnnotateAll(annotation string, errs ...error) []error

AnnotateAll returns a slice of errors representing the original errors and the supplied annotation.

func As

func As(err error, target interface{}) bool

As calls errors.As.

func Caller added in v0.0.3

func Caller(err error) error

Caller returns an error annotated with the location of its immediate caller.

Example
package main

import (
	"fmt"
	"os"

	"cloudeng.io/errors"
)

func main() {
	err := errors.Caller(os.ErrNotExist)
	fmt.Printf("%v\n", err)
	fmt.Printf("%v\n", errors.Unwrap(err))
}
Output:

errors/caller_test.go:17: file does not exist
file does not exist

func CallerAll added in v0.0.4

func CallerAll(err ...error) []error

CallerAll returns a slice conntaing annotated versions of all of the supplied errors.

func FileLocation added in v0.0.3

func FileLocation(depth, nameLen int) string

FileLocation returns the callers location as a filepath and line number. Depth follows the convention for runtime.Caller. The filepath is the trailing nameLen components of the filename returned by runtime.Caller. A nameLen of 2 is generally the best compromise between brevity and precision since it includes the enclosing directory component as well as the filename.

Example
package main

import (
	"fmt"

	"cloudeng.io/errors"
)

func main() {
	fmt.Println(errors.FileLocation(1, 1))
	fmt.Println(errors.FileLocation(1, 2))
}
Output:

caller_test.go:38
errors/caller_test.go:39

func Is

func Is(err, target error) bool

Is calls errors.Is.

func New

func New(m string) error

New calls errors.New.

func Unwrap

func Unwrap(err error) error

Unwrap calls errors.Unwrap.

Types

type M

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

M represents multiple errors. It is thread safe. Typical usage is:

errs := errors.M{} ... errs.Append(err) ... return errs.Err()

Example
package main

import (
	"fmt"
	"os"

	"cloudeng.io/errors"
)

func main() {
	m := &errors.M{}
	fmt.Println(m.Err())
	m.Append(os.ErrExist)
	m.Append(os.ErrInvalid)
	fmt.Println(m.Err())
}
Output:

<nil>
  --- 1 of 2 errors
  file already exists
  --- 2 of 2 errors
  invalid argument
Example (Caller)
package main

import (
	"fmt"
	"os"

	"cloudeng.io/errors"
)

func main() {
	m := &errors.M{}
	m.Append(errors.Caller(os.ErrExist))
	m.Append(errors.Caller(os.ErrInvalid))
	fmt.Println(m.Err())
}
Output:

  --- 1 of 2 errors
  errors/caller_test.go:27: file already exists
  --- 2 of 2 errors
  errors/caller_test.go:28: invalid argument

func (*M) Append

func (m *M) Append(errs ...error)

Append appends the specified errors excluding nil values.

func (*M) As

func (m *M) As(target interface{}) bool

As supports errors.As.

func (*M) Clone

func (m *M) Clone() *M

Clone returns a new errors.M that contains the same errors as itself.

func (*M) Err

func (m *M) Err() error

Err returns nil if m contains no errors, or itself otherwise.

func (*M) Error

func (m *M) Error() string

Error implements error.error

func (*M) Format

func (m *M) Format(f fmt.State, c rune)

Format implements fmt.Formatter.Format.

func (*M) Is

func (m *M) Is(target error) bool

Is supports errors.Is.

func (*M) Unwrap

func (m *M) Unwrap() error

Unwrap implements errors.Unwrap. It returns the first stored error and then removes that error.

Jump to

Keyboard shortcuts

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