v0.0.0-...-d9fd4a4 Latest Latest

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

Go to latest
Published: Oct 29, 2015 License: MIT Imports: 5 Imported by: 0



Typical errors in Go are often created using errors.New() or fmt.Errorf(). Those errors only contain a string as information. When trying to differentiate between errors or to carry helpful payload own types are needed.

The errors package allows to easily created formatted errors with New() like with the fmt.Errorf function, but also with an error code. Additionlly a Messages instance has to be passed to map the error code to their according messages.

If an error alreay exists use Annotate(). This way the original error will be stored and can be retrieved with Annotated(). Also its error message will be appended to the created error separated by a colon.

All errors additionally contain their package, filename and line number. These information can be retrieved using Location(). In case of a chain of annotated errors those can be retrieved as a slice of errors with Stack().



View Source
const (
	ErrInvalidErrorType = iota + 1


This section is empty.


func Annotate

func Annotate(err error, code int, msgs Messages, args ...interface{}) error

Annotate creates an error wrapping another one together with a a code.

func Annotated

func Annotated(err error) error

Annotated returns the possibly annotated error. In case of a different error an invalid type error is returned.

func DeprecatedError

func DeprecatedError(feature string) error

DeprecatedError returns the common error for a deprecated feature.

func IsDeprecatedError

func IsDeprecatedError(err error) bool

IsDeprecatedError checks if an error signals deprecated feature.

func IsError

func IsError(err error, code int) bool

IsError checks if an error is one created by this package and has the passed code

func IsInvalidTypeError

func IsInvalidTypeError(err error) bool

IsInvalidTypeError checks if an error signals an invalid type in case of testing for an annotated error.

func IsNotYetImplementedError

func IsNotYetImplementedError(err error) bool

IsNotYetImplementedError checks if an error signals a not yet implemented feature.

func Location

func Location(err error) (string, string, int, error)

Location returns the package and the file name as well as the line number of the error.

func New

func New(code int, msgs Messages, args ...interface{}) error

New creates an error with the given code.

func NotYetImplementedError

func NotYetImplementedError(feature string) error

NotYetImplementedError returns the common error for a not yet implemented feature.

func PackageVersion

func PackageVersion() version.Version

PackageVersion returns the version of the version package.

func Stack

func Stack(err error) []error

Stack returns a slice of errors down to the first non-errors error.

func Valid

func Valid(err error) bool

Valid returns true if it is a valid error generated by this package.


type Messages

type Messages map[int]string

Messages contains the message strings for the the error codes.

func (Messages) Format

func (m Messages) Format(code int, args ...interface{}) string

Format returns the formatted error message for code with the given arguments.

Jump to

Keyboard shortcuts

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