falta

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 5 Imported by: 3

README

falta

Falta is a simple error package for Go

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExtendableFactory added in v0.7.0

type ExtendableFactory[T any] interface {
	Factory[T]
	Extend(f Factory[T]) ExtendableFactory[T]
}

ExtendableFactory is an error factory that can be extended.

func NewM added in v0.6.0

func NewM(errFmt string) ExtendableFactory[M]

NewM returns a new ExtendableFactory instance using a template that expects a falta.M (i.e., map[string]any). This is a convenience function for calling falta.New[falta.M](...)

func Newf

func Newf(errFmt string) ExtendableFactory[any]

Newf creates a new Falta instance that will construct errors using the printf format string provided.

type Factory

type Factory[T any] interface {
	error
	New(vs ...T) Falta
}

Factory is an error factory.

Example
package main

import (
	"fmt"

	"github.com/a20r/falta"
)

type Circle struct {
	Radius float64
}

var ErrInvalidCircle = falta.New[Circle]("invalid circle: radius ({{.Radius}}) <= 0")

func IsCircleValid(circle Circle) error {
	if circle.Radius <= 0 {
		return ErrInvalidCircle.New(circle)
	}

	return nil
}

func main() {
	circle := Circle{Radius: -1}

	if err := IsCircleValid(circle); err != nil {
		fmt.Println(err)
	}

}
Output:

invalid circle: radius (-1) <= 0

func New

func New[T any](errFmt string) Factory[T]

New creates a new Falta instance that construct errors by executing the provided template string on a struct of the type provided.

type Falta

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

Falta is an error returned by the Factory

func NewError added in v0.5.0

func NewError(msg string) Falta

NewError returns a new Falta error type with the provided error string.

NOTE (a20r, 2024-02-25): It panics if the provided message contains any fmt verbs.

func (Falta) Annotate added in v0.2.0

func (f Falta) Annotate(annotation string) Falta

Annotate adds an annotation to the error to provide more context to why it's happening

NOTE (a20r, 2024-02-25): It panics if the provided annotation contains any fmt verbs.

func (Falta) Capture added in v0.4.0

func (f Falta) Capture(err *error)

Capture captures the error provided and wraps it with the Falta instance if it's not nil. This should be called with defer at the top of the function for which you are trying to capture the error. This ensures that all errors returned from your function will be wrapped by the function passed into Capture. You should use a named return value for the error so that the error Capture wraps is the one returned from teh function.

func (Falta) Is added in v0.1.0

func (f Falta) Is(err error) bool

Is returns true if the error provided is a Falta instance created by the same factory.

func (Falta) Unwrap added in v0.1.2

func (f Falta) Unwrap() error

Unwrap returns the wrapped error if there is one.

func (Falta) Wrap

func (f Falta) Wrap(err error) Falta

Wrap wraps the error provided with the Falta instance.

type M added in v0.6.0

type M map[string]any

M is a convenience type for using Falta instances with maps.

Jump to

Keyboard shortcuts

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