tre

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 8 Imported by: 13

README

tre - tracing error in the call stack

Package tre has the TracingError type to collect stack information when an error is caught.

GoDoc Go Report Card

usage

func foo(param string) error {
    if err := someOperation(); err != nil {
        return tre.New(err,"foo failed", "param", param)
    }
}

func bar() error {
    info := map[string]interface{}
    // ...doing stuff
    info["some"] = "thing"
    // ...doing more stuff
    info["more"] = 2
    // ...almost done
    info["done"] = true

    // ...then an issue arises
    if err != nil {
        // Answer error containing the full context object as key value pairs
        return tre.New(err, "bar failed", info)
    }

    return nil
}

(c) 2016, http://ernestmicklei.com. MIT License

Documentation

Overview

Package tre has the TracingError type to collect stack information when an error is caught.

It is inspired the minio probe package ; this one is leaner and has no external dependencies.

func main() {
	err := doSomething("demo")
	println(tre.New(err, "doSomething failed").Error())
}

func doSomething(with string) error {
	err := doAnotherThingThatCanFail(with)
	return tre.New(err, "doAnotherThingThatCanFail failed", "with", with) // pass error, message and context
}

func doAnotherThingThatCanFail(with string) error {
	return errors.New("something bad happened")
}

The TracingError Error() function returns a verbose output of stack information including file,line,function,message and custom key,values.

main.go:11 main.main:doSomething failed
main.go:16 main.doSomething:doAnotherThingThatCanFail failed with=demo
something bad happened

(c) 2016, http://ernestmicklei.com. MIT License

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the initiating error by recursively seeking it.

func New

func New(err error, msg string, kv ...interface{}) error

New creates a TracingError with a failure message and optional context information. It accepts either an error, a TracingError or nil. If the error is nil then return nil. Context information can either be provided as keys and values (e.g. "key1", value1, "key2", value2, etc) or can be provided as a map[string]interface{} context object.

Types

type CompositeError added in v1.4.0

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

CompositeError holds errors

func NewErrors added in v1.4.0

func NewErrors() *CompositeError

NewErrors returns a new CompositeError to collect errors and build a single error.

func (*CompositeError) Add added in v1.4.0

func (c *CompositeError) Add(err error) *CompositeError

Adds a an error to the list unless it is nil.

func (*CompositeError) Err added in v1.4.0

func (c *CompositeError) Err() error

Err creates a new error with a message composed of each error

func (*CompositeError) Errorf added in v1.4.0

func (c *CompositeError) Errorf(msg string, args ...any) *CompositeError

Errorf add a new error.

type TracingError

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

TracingError encapsulates an error and collects tracing information back to the point where it is handled (logged,ignored,responded...).

func (TracingError) As added in v1.2.0

func (e TracingError) As(target interface{}) bool

As assists with errors.Unwrap

func (TracingError) Cause

func (e TracingError) Cause() error

Cause returns the initiating error.

func (TracingError) Error

func (e TracingError) Error() string

Error returns a pretty report of this error.

func (TracingError) LoggingContext

func (e TracingError) LoggingContext() map[string]interface{}

LoggingContext collects all data for context aware logging purposes. Fixed keys are {err,err.type,msg,stack} unless the value is empty.

func (TracingError) Unwrap added in v1.2.0

func (e TracingError) Unwrap() error

Unwrap to be able to easily nest/unnest

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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