echain

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 2 Imported by: 10

README

error-chain

With golang 1.13 the introduction of error wrapping was introduced.

There may be a need to gather an array of errors. With this package you can do just that and leverage the new 1.13 functionality.

package main

import (
	"errors"
	"fmt"

	chain "github.com/g8rswimmer/error-chain"
)

type myError struct {
	code int
}

func (e *myError) Error() string {
	return fmt.Sprintf("%d", e.code)
}

func (e *myError) Is(target error) bool {
	te, ok := target.(*myError)
	if ok == false {
		return false
	}
	return e.code == te.code
}

func someFunction() error {
	ec := chain.New()
	ec.Add(errors.New("some error"))
	ec.Add(fmt.Errorf("wrap it up %w", &myError{code: 12}))
	return ec
}

func otherFunction() {
	err := someFunction()
	if errors.Is(err, &myError{code: 12}) {
		fmt.Println("got an error")
	}
}

func main() {
	fmt.Println("runing the error chain example")
	otherFunction()
}

Output:

▶ go run *.go
runing the error chain example
got an error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorChain

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

ErrorChain will chain multiple errors

func New

func New() *ErrorChain

New create a new error chain

func (*ErrorChain) Add

func (e *ErrorChain) Add(err error)

Add will place another error in the chain

func (*ErrorChain) Error

func (e *ErrorChain) Error() string

func (*ErrorChain) Errors

func (e *ErrorChain) Errors() []error

Errors will return the errors in the chain

func (*ErrorChain) Is

func (e *ErrorChain) Is(target error) bool

Is will comapre the target

func (*ErrorChain) Unwrap

func (e *ErrorChain) Unwrap() error

Unwrap will give the next error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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