Skip to content

natefinch/eg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eg

import "github.com/natefinch/eg"

Package eg implements improved error handling mechanisms.

This package solves several common problems with Go's native error handling:

Tracebacks with context to help understand where an error came from.

The ability to wrap an error with a new error without losing the context of the original error.

A way to print out more detailed information about an error.

A way to mask some or all of the errors coming out of a function with anonymous errors to prevent deeep coupling.

Examples:

type NotFoundError struct {
	*eg.Err
}

func IsNotFound(err error) bool {
	_, ok := err.(NotFoundError)
	return ok
}

func GetConfig() []byte, error {
	data, err := ioutil.ReadFile("config_file")
	if os.IsNotExists(err) {
		// Return a new error with the original error as the cause.
		return nil, NotFoundError{eg.Err{CauseErr: err, Message: "Couldn't find config file"}}
	}
	if err != nil {
		// Return a generic error for other problems.
		return eg.Note(err, "Error reading config file")
	}
	return data, nil
}

func StartFoo() error {
	data, err := GetConfig()
	if err != nil {
		return eg.Note(err, "Can't start foo")
	}
	return nil
}

func Bootstrap() error {
	err := StartFoo()
	if err != nil {
		// Add context to the error.
		return eg.Note(err, "Can't bootstrap")
	}
	return nil
}

func main() {
	err := Bootstrap()
	fmt.Printf("%v", err)
}

// Output:
// Can't bootstrap: Can't start foo: Couldn't find config file: open config_file: file or directory not found

func Cause

func Cause(err error) (cause error, ok bool)

Cause returns the cause of the error. If the error has a cause, ok will be true, and cause will contain the cause. Otherwise the err will be returned as the cause.

func Details

func Details(err error) string

Details returns detailed information about the error, or the error's Error() string if no detailed information is available.

func Mask

func Mask(err error, msg string, args ...interface{}) error

Mask returns a new Err object with a message based on the given error's message but without listing the error as the Cause.

func Note

func Note(err error, msg string, args ...interface{}) error

Note annotates the error if it is already an Annotable error, otherwise it wraps the error in an Err using msg as the error's message.

type Annotatable

type Annotatable interface {
    Annotate(msg, function, file string, line int) error
}

Annotatable is an interface that represents an error that can aggregate messages with associated locations in source code.

type Detailed

type Detailed interface {
    Details() string
}

Detailed is an interface that represents an error that can returned detailed information.

type Effect

type Effect interface {
    Cause() error
}

Effect is an interface that represents an error that can have a cause.

type Err

type Err struct {
    Message     string
    Location    location
    CauseErr    error
    Annotations []annotation
}

Err is an an error that implements Annotatable, Effect, and Detailed.

func Error

func Error(msg string, args ...interface{}) *Err

Error returns a new Err object with the given message.

func (*Err) Annotate

func (e *Err) Annotate(msg, function, file string, line int)

Annotate adds the message to the list of annotations on the error. If msg is empty, the annotation will only be displayed when printing the error's details.

func (*Err) Cause

func (e *Err) Cause() error

Cause returns the error object that caused this error.

func (*Err) Details

func (e *Err) Details() string

Details returns a detailed list of annotations including files and line numbers.

func (*Err) Error

func (e *Err) Error() string

Error implements the error interface.


Generated by godoc2md

About

An enhanced error package for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages