evs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 5 Imported by: 1

README

evs

GoDoc Test

package evs (Error ValueS) is another error generating package. It aims to be simple and yet contain a useful set of features such as, stack traces, custom formatting, and full compatibility with the standard library post Go 1.13.

Example Usage

Create new errors with the New function:

func MyFancyFunction() error {
    return evs.New("something went terribly wrong").Err()
}

Enhance existing errors (or return nil if there was no error):

func MyFancyFunction() error {
    _, err := fmt.Println("hello, world")
    return evs.From(err).Err()
}

Custom Formatting

You can set your own formatting for these errors! There are two formatters that are built in for now but you can easily create your own. The formatters themselves are not exported (trying to keep the surface area of the package as small as possible) but functions to set them are exported. If you want to change formatting for all errors across the board, do something like this:

evs.GetFormatterFunc = evs.JSONFormatter  // by default it is set to the TextFormatter

Additionally, you can set the formatter on a per-error basis like this:

err := evs.New("uh oh").Fmt(JSONFormatter()).Err()

More generally, you can implement your own by implementing a evs.Formatter which will format your error however you'd like.

Documentation

Index

Examples

Constants

View Source
const (
	KindIO      = "IO"
	KindType    = "Type"
	KindUnknown = ""
	KindValue   = "Value"
)

Variables

View Source
var (
	// IncludeStack is used to determine whether or not a stacktrace should be captured with
	// new errors. By default it is set to true.
	IncludeStack = true
	// InspectFull controls how [From] operates. By default, the full error stack will be inspected
	// via [errors.As]. If any [evs.Error] exists within the stack, that error is extracted and returned.
	// You can turn this behavior off, by setting InspectFull to false. This will then only check the
	// error itself (without calling unwrap).
	InspectFull = true
)
View Source
var (
	// GetFormatterFunc should return the formatter that gets used in each instantiation of an error. You can
	// supply your own implementation if you would like to change how errors are formatted. See the source
	// code for the [textFormatter] to see how it is implemented.
	GetFormatterFunc = TextFormatter
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Wraps   error
	Stack   Stack
	Details []string
	Kind    Kind
	// contains filtered or unexported fields
}

Error implements both the Error interface as well as Unwrap.

func (*Error) Error

func (err *Error) Error() string

Error implements the error interface.

func (*Error) Format

func (err *Error) Format(state fmt.State, verb rune)

Format implements the fmt.Formatter interface.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap allows you to unwrap any internal error which makes the implementation compatible with errors.As.

type Formatter

type Formatter interface {
	Format(e *Error, f fmt.State, verb rune)
}

Formatter is almost the same as the fmt.Formatter but passes the Error in as well.

func TextFormatter

func TextFormatter() Formatter

TextFormatter returns the Formatter that is used by default. You can use this to restore default behavior if you swapped in your own Formatter at some point.

type Frame

type Frame struct {
	Line     int
	File     string
	Function string
}

Frame defines a single frame in a stack trace.

func CurrentFrame

func CurrentFrame(skip int) Frame

CurrentFrame gets the location information for the code point where this function was called from (or anywhere up or down the stack from there depending on the skip value given.)

type Kind

type Kind string

func KindOf

func KindOf(err error) Kind

KindOf returns the Kind of this error. If it cannot determine the Kind (e.g. because maybe the provided error is not an evs.Error) it returns KindUnknown.

type Record

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

Record is a builder type that is used to build up an Error. Once you have created the error the way you want it to exist, call Record.Err to return an error type.

func From

func From(err error) *Record

From generates a new record from the given error. If the error is nil, the record will contain a nil internal Error. If the given error is not nil, it first checks to see if it already contains a Error. If it does, it directly sets the underlying Error to that error. Otherwise, it creates a new Error that wraps the given error. Use this method if you don't intend to "wrap" [Error]s but rather just have one single error that you can pass around.

Example
package main

import (
	"errors"
	"log"

	"github.com/thenorthnate/evs"
)

func main() {
	err := errors.New("something terrible happened!")
	newErr := evs.From(err).Err()
	if newErr == nil {
		log.Fatal("This should be an error!")
	}
}

func New

func New(msg string) *Record

New creates a new Record with the given message and the Std error type.

Example
package main

import (
	"log"

	"github.com/thenorthnate/evs"
)

func main() {
	err := evs.New("something terrible happened!").Err()
	if err == nil {
		log.Fatal("This should be an error!")
	}
}

func Newf

func Newf(msg string, args ...any) *Record

Newf creates a new Record with the given formatted message and the Std error type.

func (*Record) DropStack

func (rec *Record) DropStack() *Record

DropStack allows you to remove the stacktrace for this specific error. You might use this if you generally want the full stacktrace for everything (thus [IncludeStack]==true), but you don't want this error to have it.

func (*Record) Err

func (rec *Record) Err() error

Err returns the error that you've built up via the other methods.

func (*Record) Fmt

func (rec *Record) Fmt(f Formatter) *Record

Fmt allows you to set the Formatter you'd like to use which dictates how the messages are printed out.

func (*Record) Kind added in v0.2.0

func (rec *Record) Kind(k Kind) *Record

Kind sets the error kind.

func (*Record) Msg

func (rec *Record) Msg(msg string) *Record

Msg provides a mechanism to set the error message directly.

func (*Record) Msgf

func (rec *Record) Msgf(msg string, args ...any) *Record

Msgf is the same as Record.Msg except that it takes a variadic set of arguments.

func (*Record) Set

func (rec *Record) Set(wraps error) *Record

Set directly assigns the given error to the internal wrapped error. It overrides any previously wrapped error that may have already been in place.

type Stack

type Stack struct {
	Frames []Frame
}

Stack contains a stack trace made up of individual frames.

func GetStack

func GetStack(skip int) Stack

GetStack returns the full set of frames excluding the frames within the evs package assuming an appropriate value for Skip has been supplied. To get the stack excluding the call to GetStack itself (and everything beneath it), the value for skip should be 0.

Jump to

Keyboard shortcuts

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