Discover Packages
github.com/wspowell/errors
package
module
Version:
v0.5.2
Opens a new window with list of versions in this module.
Published: Aug 21, 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 686074262 1.676 ns/op 0 B/op 0 allocs/op
BenchmarkGoerrorsNew-12 44342046 26.80 ns/op 16 B/op 1 allocs/op
BenchmarkErrFunc-12 655313150 1.772 ns/op 0 B/op 0 allocs/op
BenchmarkGoerrorsFunc-12 42196820 26.93 ns/op 16 B/op 1 allocs/op
BenchmarkErrFormat-12 23672893 49.89 ns/op 4 B/op 1 allocs/op
BenchmarkGoerrorsFormat-12 15810512 75.96 ns/op 20 B/op 2 allocs/op
BenchmarkErrorInto-12 1000000000 1.105 ns/op 0 B/op 0 allocs/op
BenchmarkGoerrorsWrap-12 7936600 166.1 ns/op 36 B/op 2 allocs/op
PASS
ok github.com/wspowell/errors 11.275s
goos: linux
goarch: amd64
pkg: github.com/wspowell/errors/result
cpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
BenchmarkResultOk-12 100574 11109 ns/op 0 B/op 0 allocs/op
BenchmarkResultErr-12 47370 24312 ns/op 0 B/op 0 allocs/op
BenchmarkOkResult-12 59538 20152 ns/op 0 B/op 0 allocs/op
BenchmarkGoerrorOk-12 352614 3323 ns/op 0 B/op 0 allocs/op
BenchmarkErrResult-12 37786 31357 ns/op 0 B/op 0 allocs/op
BenchmarkGoerrorErr-12 4060 274782 ns/op 160000 B/op 10000 allocs/op
PASS
ok github.com/wspowell/errors/result 7.918s
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.
None error instance.
Creates an 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
Some 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.Some(ErrSome)
}
func main() {
if err := ErrFunc(); err.IsSome() {
// Handle error.
fmt.Println(err)
return
}
fmt.Println("no error")
}
Output:
WHOOPS
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.
Source Files
¶
Directories
¶
Click to show internal directories.
Click to hide internal directories.