errors

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 1 Imported by: 23

README

Coverage Status Go Report Card GoDoc GitHub license

errors

Create error tree.

Installation
go get -u github.com/Konstantin8105/errors
Example
type ErrorValue struct {
	ValueName string
	Reason    error
}

func (e ErrorValue) Error() string {
	return fmt.Sprintf("Value `%s`: %v", e.ValueName, e.Reason)
}

func Example() {
	// some input data
	f := math.NaN()
	i := -32
	var s string

	// checking
	var et Tree
	et.Name = "Check input data"
	if math.IsNaN(f) {
		et.Add(ErrorValue{
			ValueName: "f",
			Reason:    fmt.Errorf("is NaN"),
		})
	}
	if f < 0 {
		et.Add(fmt.Errorf("Parameter `f` is negative"))
	}
	if i < 0 {
		et.Add(fmt.Errorf("Parameter `i` is less zero"))
	}
	if s == "" {
		et.Add(fmt.Errorf("Parameter `s` is empty"))
	}

	if et.IsError() {
		fmt.Println(et.Error())
	}

	// walk
	Walk(&et, func(e error) {
		fmt.Fprintf(os.Stdout, "%-25s %v\n", fmt.Sprintf("%T", e), e)
	})

	// Output:
	// Check input data
	// ├──Value `f`: is NaN
	// ├──Parameter `i` is less zero
	// └──Parameter `s` is empty
	//
	// errors.ErrorValue         Value `f`: is NaN
	// *errors.errorString       Parameter `i` is less zero
	// *errors.errorString       Parameter `s` is empty
}

Acceptable add in error tree another error tree and possibly look like that:

+
├── Error 0
├── +
│   ├── Inside error 0
│   └── Some deep deep errors
│       └── Deep error 0
├── Error 1
├── Error 2
├── Error 3
├── +
│   ├── Inside error 0
│   ├── Some deep deep errors
│   │   └── Deep error 0
│   └── Inside error 1
├── Error 4
├── Error 5
├── Error 6
├── +
│   ├── Inside error 0
│   ├── Some deep deep errors
│   │   └── Deep error 0
│   ├── Inside error 1
│   ├── Inside error 2
│   └── Some deep deep errors
│       ├── Deep error 0
│       └── Deep error 1
├── Error 7
├── Error 8
├── Error 9
└── +
    ├── Inside error 0
    ├── Some deep deep errors
    │   └── Deep error 0
    ├── Inside error 1
    ├── Inside error 2
    ├── Some deep deep errors
    │   ├── Deep error 0
    │   └── Deep error 1
    └── Inside error 3

Documentation

Overview

Example
// some input data
f := math.NaN()
i := -32
var s string

// checking
var et Tree
et.Name = "Check input data"
if math.IsNaN(f) {
	et.Add(ErrorValue{
		ValueName: "f",
		Reason:    fmt.Errorf("is NaN"),
	})
}
if f < 0 {
	et.Add(fmt.Errorf("Parameter `f` is negative"))
}
if i < 0 {
	et.Add(fmt.Errorf("Parameter `i` is less zero"))
}
if s == "" {
	et.Add(fmt.Errorf("Parameter `s` is empty"))
}

if et.IsError() {
	fmt.Println(et.Error())
}

// walk
Walk(&et, func(e error) {
	fmt.Fprintf(os.Stdout, "%-25s %v\n", fmt.Sprintf("%T", e), e)
})
Output:

Check input data
├──Value `f`: is NaN
├──Parameter `i` is less zero
└──Parameter `s` is empty

errors.ErrorValue         Value `f`: is NaN
*errors.errorString       Parameter `i` is less zero
*errors.errorString       Parameter `s` is empty

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(t *Tree, f func(error))

Walk walking by error tree

Types

type Tree

type Tree struct {
	Name string
	// contains filtered or unexported fields
}

Tree is struct of error tree

Example
et := New("Check error tree")
for i := 0; i < 2; i++ {
	et.Add(fmt.Errorf("Error case %d", i))
}
fmt.Println(et.Error())

// walk
Walk(et, func(e error) {
	fmt.Fprintf(os.Stdout, "%T %v\n", e, e)
})
Output:

Check error tree
├──Error case 0
└──Error case 1

*errors.errorString Error case 0
*errors.errorString Error case 1

func New

func New(name string) *Tree

New create a new tree error

func (*Tree) Add

func (e *Tree) Add(err error) *Tree

Add error in tree node

func (Tree) Error

func (e Tree) Error() (s string)

Error is typical function for interface error

func (Tree) IsError

func (e Tree) IsError() bool

IsError check have errors in tree

func (*Tree) Reset

func (e *Tree) Reset()

Reset errors in tree

Jump to

Keyboard shortcuts

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