errorglue

package
v0.4.171 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: ISC Imports: 11 Imported by: 0

Documentation

Overview

Package errorglue contains non-essential error declarations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChainString

func ChainString(err error, format CSFormat) (s string)

ChainString() gets a string representation of a single error chain TODO 220319 finish comment

func DumpChain

func DumpChain(err error) (typeNames string)

DumpChain retrieves a space-separated string of error implementation type-names found in the error chain of err. err can be nil

fmt.Println(Stack(errors.New("an error")))
*error116.errorStack *errors.errorString

func DumpGo

func DumpGo(err error) (typeNames string)

DumpGo produces a newline-separated string of type-names and Go-syntax found in the error chain of err. err can be nil

func ErrorChainSlice

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

ErrorChainSlice returns a slice of errors from a possible error chain. If err is nil, an empty slice is returned. If err does not have an error chain, a slice of only err is returned. Otherwise, the slice lists each error in the chain starting with err at index 0 ending with the oldest error of the chain

func ErrorList

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

ErrorList returns the list of associated errors enclosed in all error chains of err

  • the returned slice is a list of error chains beginning with the initial err
  • If err is nil, a nil slice is returned
  • duplicate error values are ignored
  • order is:
  • — associated errors from err’s error chain, oldest first
  • — then associated errors from other error chains, oldest found error of each chain first, and errors from the last found error chain first
  • associated errors are independent error chains that allows a single error to enclose addditional errors not part of its error chain
  • cyclic error values are filtered out so that no error instance is returned more than once

func ErrorsWithStack

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

ErrorsWithStack gets all errors in the err error chain that has a stack trace. Oldest innermost stack trace is returned first. if not stack trace is present, the slice is empty

func FirstPanicStack added in v0.4.154

func FirstPanicStack(err error) (
	isPanic bool, stack pruntime.Stack, recoveryIndex, panicIndex int,
	numberOfStacks int,
	errorWithStack error,
)

FirstPanicStack checks all stack traces, oldest first, for a panic and returns:

  • — the panic stack if any
  • — otherwise, the oldest stack if any
  • this enables consumer to print:
  • — panic location if any
  • — otherwise, oldest error location if any
  • — otherwise no location
  • err is an error chain that may contain stack traces and be nil
  • if any stack trace has a panic:
  • — isPanic is true
  • — stack is the oldest stack with a panic
  • — stack[panicIndex] is the code line causing the oldest panic
  • — stack[recoveryIndex] is the first code line in the deferred sequence. Typically the last line of the executing function
  • — numberOfStacks is 1 for the panic stack and added the number of stacks that are older
  • — errorWithStack is the error with panic, typically from [parl.Recover]
  • if isPanic is false:
  • — panicIndex recoveryIndex are zero
  • — stack and errorWithStack is the oldest stack if any -— numberOfStacks is total number of stacks in the error chain
  • this must be done beacuse maybe panic() was provided an error with stack trace
  • on err nil: all values false, nil or zero

func GetInnerMostStack

func GetInnerMostStack(err error) (stack pruntime.Stack)

GetInnerMostStack gets the oldest stack trace in the error chain or nil if no stack trace is present

func GetStackTrace

func GetStackTrace(err error) (stack pruntime.Stack)

GetStackTrace gets the last stack trace

func GetStacks

func GetStacks(err error) (stacks []pruntime.Stack)

GetStacks gets a slice of all stack traces, oldest first

func IsMinusFlag

func IsMinusFlag(s fmt.State) bool

IsMinusFlag determines if fmt.State has the '-' flag

func IsPlusFlag

func IsPlusFlag(s fmt.State) bool

IsPlusFlag determines if fmt.State has the '+' flag

func IsQuoteVerb

func IsQuoteVerb(r rune) bool

IsQuoteVerb determines if the rune corresponds to the %q quote verb

func IsStringVerb

func IsStringVerb(r rune) bool

IsStringVerb determines if the rune corresponds to the %s string verb

func IsValueVerb

func IsValueVerb(r rune) bool

IsValueVerb determines if the rune corresponds to the %v value verb

func NewErrorData

func NewErrorData(err error, key, value string) (e2 error)

NewErrorData returns an error containg a key/value pair

func NewErrorStack

func NewErrorStack(err error, st pruntime.Stack) (e2 error)

NewErrorStack attaches a stack to err

func NewRelatedError

func NewRelatedError(err, err2 error) (e2 error)

func NewWarning

func NewWarning(err error) error

func RecoverThread

func RecoverThread(label string, onError func(err error))

RecoverThread is a defer function for threads. On panic, the onError function is invoked with an error message that contains location information

Types

type CSFormat

type CSFormat uint8

CSFormat describes string conversion of an error chain

  • DefaultFormat ShortFormat LongFormat ShortSuffix LongSuffix
const (
	// DefaultFormat is like printf %v, printf %s and error.Error() “message”
	//	- data, stack traces and associated errors are not printed
	//	- code location is not printed
	DefaultFormat CSFormat = iota + 1
	// CodeLocation DefaultFormat with location added “message at runtime/panic.go:914”
	//	- location is:
	//	- — oldest panic code-line in any stack trace of err or its error chain
	//	- — code-line creating the oldest error in err and its chain that has
	//		a stack trace, when none of the errors hold a panic
	CodeLocation
	// ShortFormat has one-line location similar to printf %-v
	//	- data, and stack traces are not printed
	//	- associated errors are printed
	//	- if the error or its chain contains a stack trace, a code location is output
	//	- if any stack trace contains a panic, that oldest panic location is printed
	//	- “error-message at error116.(*csTypeName).FuncName-chainstring_test.go:26”
	ShortFormat
	// LongFormat is similar to printf %+v.
	//	- prints data, stack traces and associated errors
	//	- if any stack trace contains a panic, that oldest panic location is printed after message
	//
	// output:
	//	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
	LongFormat
	// ShortSuffix is code-location “runtime/panic.go:914”
	//	- no leading “ at ”
	//	- if no stack is present, empty string
	ShortSuffix
	// LongSuffix full stack trace without message
	LongSuffix
)

func PrintfFormat

func PrintfFormat(s fmt.State) CSFormat

PrintfFormat gets the ErrorFormat to use when executing the Printf value verb 'v'

func (CSFormat) String

func (csFormat CSFormat) String() (s string)

type ChainStringer

type ChainStringer interface {
	// ChainString is used by ChainStringer() to obtain comprehensive
	// string representations of errors.
	// The argument isIgnore() is used to avoid printing cyclic error values.
	// If a ChainStringer pointer receiver gets a nil value, the empty string is returned.
	// ChainString() obtains a string representation of the errors in its chain.
	// Rich errors implement either ChainStringer or fmt.Formatter
	ChainString(format CSFormat) string
}

ChainStringer obntain s a comprehensive string representation of an error chain. formats used are DefaultFormat ShortFormat LongFormat ShortSuffix LongSuffix

type ErrorCallStacker

type ErrorCallStacker interface {
	StackTrace() (stack pruntime.Stack)
}

ErrorCallStacker enrichens an error with a stack trace of code locations

type ErrorChain

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

ErrorChain implements a chain of errors. Error chains do exist in the Go standard library but types and interfaces are not public. ErrorChain is used as an embedded type. ErrorChain’s publics are Error() and Unwrap()

ErrorChainSlice returns all errors of an error chain, or the chain can be traversed iteratively using errors.Unwrap()

func (*ErrorChain) Unwrap

func (ec *ErrorChain) Unwrap() error

Unwrap is a method required to make ErrorChain an error chain ErrorChain.Unwrap() is used by errors.Unwrap() and ErrorChainSlice

type ErrorHasCode

type ErrorHasCode interface {
	// Check if this error claims a particular Linux errno, an int
	IsErrno(errno int) (hasErrno bool)
	// ErrorCode determines if this error claims code, a string
	ErrorCode(code string) (hasCode bool)
	// ErrorCodes returns codes that this error claims, some are numeric strings mapping to an errno
	ErrorCodes(codes []string) (has []string)
}

ErrorHasCode allows an error to classify itself

type ErrorHasData

type ErrorHasData interface {
	KeyValue() (key, value string)
}

ErrorHasData enrichens an error with key and value strings

type ErrorStore

type ErrorStore interface {
	AddError(err error) (e error)
	GetError() (e error)
	InvokeIfError(fn func(err error))
	Error() (message string)
}

ErrorStore is a thread-safe store for any number of errors

type RelatedError

type RelatedError interface {
	AssociatedError() (error error)
}

RelatedError enrichens an error with an enclosed additional error value

type RichError

type RichError struct {
	ErrorChain
}

RichError is an error chain that behaves like fmt.Formatter. this allows for custom print-outs using %+v and %-v RichError has publics Error() Unwrap() Format()

func (*RichError) Format

func (e *RichError) Format(s fmt.State, verb rune)

Format provides the fmt.Formatter function

type SendNb

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

SendNb implements non-blocking send using a thread and buffer up to size of int

func NewSendNb

func NewSendNb(errCh chan error) (sc *SendNb)

NewSendNb returns a buffered Send object using errCh

  • errCh may be nil

func (*SendNb) HasChannel

func (sc *SendNb) HasChannel() (hasChannel bool)

func (*SendNb) IsShutdown

func (sc *SendNb) IsShutdown() (isShutdown bool)

func (*SendNb) Send

func (sc *SendNb) Send(err error)

Send sends an error on the error channel. Non-blocking. Thread-safe.

  • if err is nil, nothing is done
  • if SendNb was not initialized with non-zero channel, nothing is done

func (*SendNb) Shutdown

func (sc *SendNb) Shutdown()

Shutdown closes the channel exactly once. Thread-safe

type WarningType

type WarningType struct {
	ErrorChain
}

warningType is an error with lesser impact

func (*WarningType) Error

func (w *WarningType) Error() (s string)

Error prepends “Warning: ” to the error message

type Wrapper

type Wrapper interface {
	Unwrap() error // Unwrap returns the next error in the chain or nil
}

Wrapper is an interface indicating error-chain capabilities. It is not public in errors package

Jump to

Keyboard shortcuts

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