Discover Packages
github.com/wspowell/errors
package
module
Version:
v0.5.3
Opens a new window with list of versions in this module.
Published: Sep 3, 2022
License: MIT
Opens a new window with license information.
Imports: 1
Opens a new window with list of imports.
Imported by: 6
Opens a new window with list of known importers.
README
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
Expand ▾
Collapse ▴
Documentation
¶
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.
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.
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
None error instance.
Creates an empty Error that IsNone().
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 (self Error [T]) Into() T
Into the error type.
Should be used when T is not a string in conjunction with switch.
IsNone return true when this error instance represents the
absence of an error.
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
Standard error is an alias for a string typed error.
Source Files
¶
Directories
¶
Click to show internal directories.
Click to hide internal directories.