errors

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2021 License: MIT Imports: 6 Imported by: 3

README

errors package

simple structured error handling, tracing, and composition

import(
    "fmt"
    "github.com/JKhawaja/errors"
)

func outerFunc(arg1 int) error {
    err := innerfunc(arg1)
    if err != nil {
        // wrap an error
        return errors.New(err, nil)
    }
    
    return nil
}

func innerFunc(arg1 int) error {
    if arg1 < 0 {
        // define a simple error, with arbitrary variable values in scope
        return errors.New(fmt.Errorf("negative argument"), map[string]interface{}{
            "arg1": arg1,
        })
    }
    return nil
}

func main() {
    err := outerFunc(-1)
    if err != nil {
        // log the trace of the error
        fmt.Println(errors.NewTrace(err).Error())
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(value error, scope map[string]interface{}) error

New will create a structured error object and return it as an `error` value.

func NewTrace

func NewTrace(val error) error

NewTrace --

Types

type Error

type Error struct {
	File      string                 `json:"file"`
	Function  string                 `json:"func"`
	Line      int                    `json:"line"`
	Package   string                 `json:"pkg"`
	Timestamp *time.Time             `json:"time,omitempty"`
	Value     *error                 `json:"value,omitempty"`
	Scope     map[string]interface{} `json:"scope,omitempty"`
}

Error represents a structured error object

func (*Error) Error

func (e *Error) Error() string

Error --

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap will return the error value

type Trace

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

Trace --

func (*Trace) Error

func (t *Trace) Error() string

Error --

func (*Trace) Unwrap

func (t *Trace) Unwrap() error

Unwrap --

Jump to

Keyboard shortcuts

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