Documentation
¶
Overview ¶
Package errors implements functions to manipulate errors.
Example (StackTraceFormatting) ¶
package main
import (
"fmt"
"github.com/komuw/ong/errors"
)
const expectedUser = "admin"
func login(user string) error {
if user == expectedUser {
return nil
}
return errors.New("invalid user")
}
func main() {
err := login("badGuy")
fmt.Printf("%+v", err)
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v0.0.98
As is a pass through to the same func from the standard library errors package.
func Dwrap ¶ added in v0.0.29
func Dwrap(errp *error)
Dwrap(aka deferred wrap) adds stack traces to the error. It does nothing when *errp == nil.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/komuw/ong/errors"
)
func main() {
fetchUser := func(u string) (errp error) {
defer errors.Dwrap(&errp)
users := []string{"John", "Alice", "Kamau"}
if !slices.Contains(users, u) {
return fmt.Errorf("user %s not found", u)
}
return nil
}
e := fetchUser("Emmy")
fmt.Printf("%+#v", e)
}
func Errorf ¶ added in v0.0.98
Errorf is equivalent to the one in standard library mainly in spirit.
func Is ¶ added in v0.0.98
Is is a pass through to the same func from the standard library errors package.
func Join ¶ added in v0.0.98
Join returns an error that wraps the given errors. Any nil error values are discarded. Join returns nil if every value in errs is nil. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each string.
A non-nil error returned by Join implements the Unwrap() error method. Unwrap returns an error whose text is the concatenation of each of the errs texts.
It only returns the stack trace of the first error.
Note that this function is equivalent to the one in standard library mainly in spirit. This is not a direct replacement of the standard library one.
func New ¶
New returns an error with the supplied message. It also records the stack trace at the point it was called.
Error values implement fmt.Formatter and can be formatted by the fmt package. The following verbs are supported:
%s print the error. %v see %s %+v print the error and stacktrace.
func StackTrace ¶
StackTrace returns the stack trace contained in err, if any, else an empty string.
func Unwrap ¶ added in v0.0.98
Unwrap is a pass through to the same func from the standard library errors package.
func Wrap ¶
Wrap returns err, capturing a stack trace. It is a no-op if err had already been wrapped by this library.
Example ¶
package main
import (
"fmt"
"os"
"github.com/komuw/ong/errors"
)
func main() {
opener := func(p string) error {
_, err := os.Open(p)
if err != nil {
return errors.Wrap(err)
}
return nil
}
fmt.Printf("%+v", opener("/this/file/does/not/exist.txt"))
}
Types ¶
This section is empty.