errors

package module
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: MIT Imports: 1 Imported by: 6

README

errors

Replacement for golang errors package.

Benchmarks

Take all benchmarks with a bucket of salt.

go test -bench=. -benchmem -count=1 -parallel 8 ./...
goos: linux
goarch: amd64
pkg: github.com/wspowell/errors
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkErr-12                 654749272                1.720 ns/op           0 B/op          0 allocs/op
BenchmarkGoerrorsNew-12         44907604                26.61 ns/op           16 B/op          1 allocs/op
BenchmarkErrFunc-12             650940289                1.783 ns/op           0 B/op          0 allocs/op
BenchmarkGoerrorsFunc-12        43059499                27.05 ns/op           16 B/op          1 allocs/op
BenchmarkErrFormat-12           21598695                50.30 ns/op            4 B/op          1 allocs/op
BenchmarkGoerrorsFormat-12      13807819                77.40 ns/op           20 B/op          2 allocs/op
BenchmarkErrorInto-12           1000000000               1.110 ns/op           0 B/op          0 allocs/op
BenchmarkGoerrorsWrap-12         7857031               159.0 ns/op            36 B/op          2 allocs/op
PASS
ok      github.com/wspowell/errors      10.068s
goos: linux
goarch: amd64
pkg: github.com/wspowell/errors/result
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkResultOk-12                              101038             11289 ns/op               0 B/op          0 allocs/op
BenchmarkResultErr-12                              48570             24485 ns/op               0 B/op          0 allocs/op
BenchmarkResultOkResult-12                         59952             20081 ns/op               0 B/op          0 allocs/op
BenchmarkGoerrorOk-12                             345360              3345 ns/op               0 B/op          0 allocs/op
BenchmarkResultErrResult-12                        35138             33459 ns/op               0 B/op          0 allocs/op
BenchmarkGoerrorErr-12                              4263            267203 ns/op          160000 B/op      10000 allocs/op
BenchmarkGoerrorErrThreeCallsDeep-12                 399           2924053 ns/op         1200521 B/op      50000 allocs/op
BenchmarkResultErrThreeCallsDeep-12                32073             37071 ns/op               0 B/op          0 allocs/op
BenchmarkGoerrorOkThreeCallsDeep-12                52590             22194 ns/op               0 B/op          0 allocs/op
BenchmarkResultOkThreeCallsDeep-12                 66266             17705 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/wspowell/errors/result       13.792s

Documentation

Index

Examples

Constants

View Source
const ErrNone = ""

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v0.4.0

type Error[T ~string] string

Error defined as a message of type string whose actual error type is type T. T is a type whose underlying type is string.

Errors may be generic as Error[string] or may have an explicit type. Usually, this is an enum type that allows Error[T] to be self documenting and is used in conjunction with Into() and the "exhaustive" linter.

func New

func New[T ~string](format T, values ...any) Error[T]

New error instance.

Creates a new error instance that is of error type T. Remember, the type T, not the error instance, is what determines what kind of error this represents.

Example
package main

import (
	"fmt"

	"github.com/wspowell/errors"
)

const ErrSome = "WHOOPS"

func ErrFunc() errors.Error[string] {
	return errors.New(ErrSome)
}

func main() {
	if err := ErrFunc(); err.IsSome() {
		// Handle error.
		fmt.Println(err)

		return
	}

	fmt.Println("no error")

}
Output:

WHOOPS

func None added in v0.5.0

func None[T ~string]() Error[T]

None error instance.

Creates an empty Error that IsNone().

Example
package main

import (
	"fmt"

	"github.com/wspowell/errors"
)

func OkFunc() errors.Error[string] {
	return errors.None[string]()
}

func main() {
	if err := OkFunc(); err.IsSome() {
		// Handle error.
		fmt.Println(err)

		return
	}

	fmt.Println("no error")

}
Output:

no error

func (Error[T]) Into added in v0.5.1

func (self Error[T]) Into() T

Into the error type.

Should be used when T is not a string in conjunction with switch.

func (Error[T]) IsNone added in v0.4.0

func (self Error[T]) IsNone() bool

IsNone return true when this error instance represents the absence of an error.

func (Error[T]) IsSome added in v0.5.1

func (self Error[T]) IsSome() bool

IsSome return true when this error instance represents the presence of an error.

Should be used when T is string or when checking if an error exists. Should not be used when checking errors when T is not string.

type Standard added in v0.5.3

type Standard = Error[string]

Standard error is an alias for a string typed error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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