error116

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: ISC Imports: 6 Imported by: 0

Documentation

Overview

Package error116 enrichens error values with string data, stack traces, associated errors, less severe warnings, thread-safe containers and comprehensive error string representations.

Creating errors with stack traces:

err := error116.New("error message") // adds a stacktrace contained inside of err
err = error116.Errorf("Some text: '%w'", err) // adds a stacktrace if err does not already have it
err = error116.Stackn(err, 0) // adds a stack trace even if err already has it

Enriching errors:

err = error116.AddKeyValue(err, "record", "123") // adds a key-value string, last key wins
err = error116.AddKeyValue(err, "", "2022-03-19 11:10:00") // adds a string to a list, oldest first

Encapsulating associated errors allowing for a single error value to contain multiple errors:

err = error116.AppendError(err, err2) // err2 is inside of err, can be printed and retrieved
fn := error116.Errp(&err) // fn(error) can be repeatedly invoked, aggregating errors in err
error116.ParlError.AddError(err) // thread-safe error encapsulation

Marking errors as less severe:

warning := error116.Warning(err) // warning should not terminate the thread
error116.IsWarning(err) // Determine severity of an error value

Printing rich errors:

error116.Long(err)
→ error-message
→ github.com/haraldrudell/parl/error116.(*csTypeName).FuncName
→   /opt/sw/privates/parl/error116/chainstring_test.go:26
→ runtime.goexit
→   /opt/homebrew/Cellar/go/1.17.8/libexec/src/runtime/asm_arm64.s:1133
→ record: 1234
→ Other error: Close failed
error116.Short(err)
→ error-message at error116.(*csTypeName).FuncName-chainstring_test.go:26
fmt.Println(err)
→ error-message

Can be used with Printf, but only works if last error in chain is from error116:

fmt.Printf("err: %+v", err) // same as Long()
fmt.Printf("err: %-v", err) // save as Short()

Is compatible:

fmt.Println(err) // no change
fmt.Printf("err: %v", err) // no change
err.Error() // no change
fmt.Printf("err: %s", err) // no change
fmt.Printf("err: %q", err) // no change

© 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddKeyValue added in v0.2.0

func AddKeyValue(err error, key, value string) (e error)

error116.AddKeyValue attaches a string value to err. The values can be trrioeved using error116.ErrorData(). if key is non-empty valiue is returned in a map where last key wins. if key is empty, valuse is returned in s string slice. err can be nil.

func AppendError

func AppendError(err error, err2 error) (e error)

error116.AppendError associates an additional error with err. err and err2 can be nil. Associated error instances can be retrieved using error116.AllErrors, error116.ErrorList or by printing using rich error printing of the error116 package. TODO 220319 fill in printing

func ErrorData

func ErrorData(err error) (list []string, keyValues map[string]string)

error116.ErrorData get possible string values associated with an error chain. list is a list of string values that were stored with an empty key, oldest first. keyValues are string values associated with a key string, newest key wins. err can be nil

func ErrorList

func ErrorList(err error) (errs []error)

error116.ErrorList returns all error instances from a possible error chain. If err is nil an empty slice is returned. If err does not have associated errors, a slice of err is returned. otherwise, the first error of the returned slice is err followed by other errors oldest first. Cyclic error values are dropped

func Errorf

func Errorf(format string, a ...interface{}) (err error)

error116.Errorf is similar to fmt.Errorf but ensures that the returned err has at least one stack trace associated

func Errp

func Errp(errp *error) func(e error)

error116.Errp returns a function that updates an an error pointer value with additional associated errors on subsequent invocations. It is intended to be used with parl.Recover(). for a thread-safe version, use error116.ParlError

func HasStack

func HasStack(err error) (hasStack bool)

error116.HasStack detects if the error chain already contains a stack trace

func IsWarning

func IsWarning(err error) (ok bool)

error116.IsWarning determines if an error has been flagged as a warning. error116.Warning() flags an error to be of warning level

func Long

func Long(err error) string

error116.Long() gets a comprehensive string representation similar to printf %+v and LongFormat. ShortFormat does not print stack traces, data and associated errors. Long() prints full stack traces, string key-value and list values for both the error chain of err, and associated errors and their chains

error-message
  github.com/haraldrudell/parl/error116.(*csTypeName).FuncName
    /opt/sw/privates/parl/error116/chainstring_test.go:26
  runtime.goexit
    /opt/homebrew/Cellar/go/1.17.8/libexec/src/runtime/asm_arm64.s:1133

func New

func New(s string) error

error116.New is similar to errors.New but ensures that the returned error has at least one stack trace associated

func PackFunc

func PackFunc() (packageDotFunction string)

error116.PackFunc returns the package name and function name of the caller:

error116.FuncName

func Short

func Short(err error) string

error116.Short gets a one-line location string similar to printf %-v and ShortFormat. Short() does not print stack traces, data and associated errors. Short() does print a one-liner of the error message and a brief code location:

error-message at error116.(*csTypeName).FuncName-chainstring_test.go:26

func Stack

func Stack(err error) (err2 error)

error116.Stack ensures the err has a stack trace associated. err can be nil in which nil is returned

func Stackn

func Stackn(err error, framesToSkip int) (err2 error)

error116.Stackn always attaches a new stack trace to err and allows for skipping stack frames using framesToSkip. if err is nil, no action is taken

func Warning

func Warning(err error) error

error116.Warning indicates that err is a problem of less severity than error. It is uesed for errors that are not to terminate the thread. A Warning can be detected using error116.IsWarning().

Types

type ParlError

type ParlError struct {
	errorglue.SendNb // non-blocking channel for sending errors
	// contains filtered or unexported fields
}

ParlError is a thread-safe error container

func NewParlError added in v0.2.2

func NewParlError(errCh chan<- error) (pe *ParlError)

NewParlError sends its errors on the provided error channel in addition to storing them in the error container. Thread safe. The error channel is closed by (*ParlError).Shutdown().

For ParlError instances without error channel, it is possible to instead use a composite initializer or to provide nil errCh value

func (*ParlError) AddError added in v0.2.0

func (pe *ParlError) AddError(err error) (err1 error)

AddError stores additional errors in the container. Thread-safe. Returns the current state. For a non-thread-safe version, use error116.Errp

func (*ParlError) AddErrorProc added in v0.2.0

func (pe *ParlError) AddErrorProc(err error)

AddErrorProc stores additional errors in the container It is thread-safe and has a no-return-value signature. For a non-thread-safe version, use error116.Errp

func (*ParlError) Error added in v0.2.0

func (pe *ParlError) Error() string

Error() makes ParlError behave like an error. Error is thread-safe unlike in most other Error implementations. Because code will check if ParlError is nil, which it mostly isn’t, and then invoke .Error(), it may be that Error is invoked when the error field is nil. For those situations, return “<nil>” like fmt.Print might do

func (*ParlError) GetError added in v0.2.0

func (pe *ParlError) GetError() (err error)

GetError returns the error value enclosed in ParlError. Thread-safe

Jump to

Keyboard shortcuts

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