lerr

package
v0.0.0-...-157c9c8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package lerr provides helpers for error handling

Index

Examples

Constants

View Source
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

View Source
var DefaultCtxSeperator = ": "

DefaultCtxSeperator is used to seperate the Context description from the inner error.

View Source
var LogTo func(err error)

LogTo can be set to handle errors when Log is called.

View Source
var MaxSliceErrs = 10

MaxSliceErrs limits the number of errors that will be reported by SliceErrs.Error

Functions

func Except

func Except(err error, except ...error) bool

Except returns true if err is equal to one of the exceptions.

func Log

func Log(err error, except ...error) bool

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

func Must[T any](t T, err error) T

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.

Example
package main

import ()

func main() {

}
Output:

func NewLenMismatch

func NewLenMismatch(expected, actual int) (min, max int, err error)

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

func NewNotEqual(areEqual bool, expected, actual interface{}) error

NewNotEqual will return nil if areEqual is true and will create an instance of ErrNotEqual if it is false.

func NewSliceErrs

func NewSliceErrs(lnExpected, lnActual int, fn func(int) error) error

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

func OK[T any](t T, ok bool) func(err error) T

func Panic

func Panic(err error, except ...error) bool

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 Whence

func Whence(i int) error

Whence returns ErrBadWhence if i is a value other than SeekStart(0), SeekCurrent(1) or SeekEnd(2).

func Wrap

func Wrap(err error, format string, data ...interface{}) error

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 Ctx

type Ctx struct {
	Innr      error
	Desc      string
	Seperator string
}

Ctx adds context to an error.

func (Ctx) Error

func (err Ctx) Error() string

Error fulfils error

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)

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.

func (ErrNotEqual) Error

func (e ErrNotEqual) Error() string

Error fulfills the error interface.

type ErrTypeMismatch

type ErrTypeMismatch struct {
	Expected, Actual reflect.Type
}

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 m lerr.Many
	var err error

	// nil error not added to Many
	m = m.Add(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

func NewMany

func NewMany(errs ...error) Many

func (Many) Add

func (m Many) Add(err error) Many

Add an error to the collection. If err is nil, it will not be added

func (Many) Cast

func (m Many) Cast() error

Cast to error type. If Many contains no errors, it will return nil.

func (Many) Error

func (m Many) Error() string

Error fulfills error

func (Many) First

func (m Many) First() error

type SliceErrRecord

type SliceErrRecord struct {
	// Index of the reference slice
	Index int
	Err   error
}

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

func (e SliceErrs) Append(idx int, err error) SliceErrs

Append a SliceErrRecord to SliceErrs. If err is nil it will not be appended.

func (SliceErrs) AppendF

func (e SliceErrs) AppendF(idx int, format string, args ...interface{}) SliceErrs

AppendF uses fmt.Errorf to append a SliceErrRecord to SliceErrs .

func (SliceErrs) Error

func (e SliceErrs) Error() string

Error fulfills the error interface.

type Str

type Str string

Str provides a string error that can be set to a const making it good for sentinal errors.

Example
package main

import (
	"fmt"

	"github.com/adamcolton/luce/lerr"
)

func main() {
	const ErrExample = lerr.Str("example error")
	fmt.Println(ErrExample)
}
Output:

example error

func (Str) Error

func (err Str) Error() string

Error fulfills error

Jump to

Keyboard shortcuts

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