Documentation ¶
Overview ¶
Package lerr provides helpers for error handling
Index ¶
- Constants
- Variables
- func Except(err error, except ...error) bool
- func Log(err error, except ...error) bool
- func Must[T any](t T, err error) T
- func MustFn[In, Out any](fn func(In) (Out, error)) func(In) Out
- func NewLenMismatch(expected, actual int) (min, max int, err error)
- func NewNotEqual(areEqual bool, expected, actual interface{}) error
- func NewSliceErrs(lnExpected, lnActual int, fn func(int) error) error
- func NewTypeMismatch(expected, actual interface{}) error
- func OK[T any](t T, ok bool) func(err error) T
- func Panic(err error, except ...error) bool
- func Recover(h ErrHandler)
- func Whence(i int) error
- func Wrap(err error, format string, data ...interface{}) error
- type Ctx
- type ErrBadWhence
- type ErrHandler
- type ErrLenMismatch
- type ErrNotEqual
- type ErrTypeMismatch
- type Many
- type SliceErrRecord
- type SliceErrs
- type Str
Examples ¶
Constants ¶
const ( // ErrHandlerFunc is returned from HandlerFunc if the provided handler // is not func(error), chan<- error or chan error. ErrHandlerFunc = Str("handler argument to HandlerFunc must be func(error) or chan error") )
Variables ¶
var DefaultCtxSeperator = ": "
DefaultCtxSeperator is used to seperate the Context description from the inner error.
var LogTo func(err error)
LogTo can be set to handle errors when Log is called.
var MaxSliceErrs = 10
MaxSliceErrs limits the number of errors that will be reported by SliceErrs.Error
Functions ¶
func Log ¶
Log returns true if err is not nil, even if err is in the exception list. Log will pass the err to LogTo if it is not nil and not in the exception list.
func Must ¶
Must takes a value and an error. If the error is not nil, it panics. If the error is nil, it returns only the value.
func NewLenMismatch ¶
NewLenMismatch will return a nil error if expected and actual are equal and an instance of LenMismatch if they are different. The min and max will be set to the smaller and larger of the values passed in respectivly.
func NewNotEqual ¶
NewNotEqual will return nil if areEqual is true and will create an instance of ErrNotEqual if it is false.
func NewSliceErrs ¶
NewSliceErrs creates an instance of SliceErrs by calling the provided func for every value up to Min(lnExpected, lnActual). If lnExpected and lnActual are not equal and instance of LenMismatch will be added to the start of the SliceErrs with an index of -1. If lnActual == -1 the length check is ignored.
func NewTypeMismatch ¶
func NewTypeMismatch(expected, actual interface{}) error
NewTypeMismatch will return nil if the given types are the same and returns ErrTypeMismatch if they do not.
func OK ¶
OK checks that the bool is true. If it is not, it panics with the given error. Invoking OK is a little awkward because it is intended to wrap a single call. It would be more natural to invoked lerr.OK(fn(), err), but the Go type system doesn't allow that so the equivalent call is lerr.OK(fn())(err).
func Panic ¶
Panic if err is not nil. If err is in the exception list, it will return true, but will not panic.
Example ¶
package main import ( "fmt" "io" "github.com/adamcolton/luce/lerr" ) func main() { var err error // won't panic on nil error lerr.Panic(err) err = io.EOF // won't panic when err in except args lerr.Panic(err, io.EOF) defer func() { fmt.Println(recover()) }() err = lerr.Str("this will panic") lerr.Panic(err) }
Output: this will panic
func Recover ¶
func Recover(h ErrHandler)
Recover should be invoked with defer. If it recovers an error, that will be passed into the error handler. Note that invoking lerr.Recover from within a defer will not work.
func Whence ¶
Whence returns ErrBadWhence if i is a value other than SeekStart(0), SeekCurrent(1) or SeekEnd(2).
func Wrap ¶
Wrap an error to provide context. If the error is nil, nil will be returned.
Example ¶
package main import ( "fmt" "github.com/adamcolton/luce/lerr" ) func main() { w := lerr.Wrap(nil, "No Error") fmt.Println(w) innerError := lerr.Str("TestError") w = lerr.Wrap(innerError, "Should Err %d time", 1) fmt.Println(w) }
Output: <nil> Should Err 1 time: TestError
Types ¶
type ErrBadWhence ¶
type ErrBadWhence int
ErrBadWhence indicates that a whence value other than SeekStart(0), SeekCurrent(1) or SeekEnd(2) was used.
func (ErrBadWhence) Error ¶
func (e ErrBadWhence) Error() string
Error fulfills the error type and indicates what bad value was used.
type ErrHandler ¶
type ErrHandler func(error)
ErrHandler is a function that can handle an error.
func HandlerFunc ¶
func HandlerFunc(handler any) (fn ErrHandler, err error)
HandlerFunc return an ErrHandler. If the errHandler argument is an ErrHandler, that will be returned. If it is an error channel then that will be wrapped in a function and returned.
func (ErrHandler) Handle ¶
func (fn ErrHandler) Handle(err error) (isErr bool)
Handle passes err into ErrHandler if both ErrHandler and err are not nil. Returns a bool indicating if err was nil.
type ErrLenMismatch ¶
type ErrLenMismatch struct {
Expected, Actual int
}
ErrLenMismatch represents a mis-matched length.
func LenMismatch ¶
func LenMismatch(expected, actual int) ErrLenMismatch
LenMismatch returns an ErrLenMismatch.
func (ErrLenMismatch) Error ¶
func (e ErrLenMismatch) Error() string
Error fulfills the error interface.
type ErrNotEqual ¶
type ErrNotEqual struct {
Expected, Actual interface{}
}
ErrNotEqual is used to indicate two values that were expected to be equal. were not.
func NotEqual ¶
func NotEqual(expected, actual interface{}) ErrNotEqual
NotEqual creates an instance of ErrNotEqual.
type ErrTypeMismatch ¶
ErrTypeMismatch indicates that two types that were expected to be equal were not.
func TypeMismatch ¶
func TypeMismatch(expected, actual interface{}) ErrTypeMismatch
TypeMismatch creates an instance of ErrTypeMismatch.
func (ErrTypeMismatch) Error ¶
func (e ErrTypeMismatch) Error() string
Error fulfills the error interface.
type Many ¶
type Many []error
Many allows many errors to be collected.
Example ¶
package main import ( "fmt" "github.com/adamcolton/luce/lerr" ) func main() { var err error // nil error not added to Many m := lerr.NewMany(err) // <nil> fmt.Println(m.Cast()) fmt.Println("---") // when many contains a single error, only that is returned from cast err = lerr.Str("first error") m = m.Add(err) fmt.Println(m.Cast()) fmt.Println("---") err = lerr.Str("second error") m = m.Add(err) fmt.Println(m.Cast()) }
Output: <nil> --- first error --- first error second error
type SliceErrRecord ¶
SliceErrRecord represents an error comparing two slices. An index less than 0 is treated as an error on the underlying comparison and will be printed without the index. For instance, if the lengths of the slices are not the same.
type SliceErrs ¶
type SliceErrs []SliceErrRecord
SliceErrs collects SliceErrRecord and treats them as a single error.
func (SliceErrs) Append ¶
Append a SliceErrRecord to SliceErrs. If err is nil it will not be appended.