falta

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 4 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 Factory

type Factory[T any] interface {
	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.

func Newf

func Newf(errFmt string) Factory[any]

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

type Falta

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

Falta is an error returned by the Factory

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

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 Fields

type Fields map[string]any

Fields 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