# README
goerr
Package goerr adds additional error handling capabilities to go.
Looking for v1, see the master branch
Quick Start
go get -u github.com/brad-jones/goerr/v2
package main
import (
"github.com/brad-jones/goerr/v2"
)
// Simple re-useable error types can be defined like this.
// This is essentially the same as "errors.New()" but creates a `*goerr.Error`.
var errFoo = goerr.New("expecting 123456789")
func crash1(abc string) error {
if err := crash2(abc + "456"); err != nil {
// Use Wrap anywhere you would normally return an error
// This will both store stackframe information and wrap the error
return goerr.Wrap(err)
}
return nil
}
func crash2(abc string) error {
if err := crash3(abc + "7810"); err != nil {
return goerr.Wrap(err)
}
return nil
}
func crash3(abc string) error {
if abc != "123456789" {
// Additional context messages can be added to the trace.
// These messages should be human friendly and when prefixed
// to the existing error message should read like a sentence.
return goerr.Wrap(errFoo, "crash3 received "+abc)
}
return nil
}
func main() {
if err := crash1("123"); err != nil {
goerr.PrintTrace(err)
}
}
Running the above will output something similar to:
crash3 received 1234567810: expecting 123456789
main.crash3:C:/Users/brad.jones/Projects/Personal/goerr/examples/simple/main.go:32
return goerr.Wrap(errFoo, "crash3 received "+abc)
main.crash2:C:/Users/brad.jones/Projects/Personal/goerr/examples/simple/main.go:22
return goerr.Wrap(err)
main.crash1:C:/Users/brad.jones/Projects/Personal/goerr/examples/simple/main.go:15
return goerr.Wrap(err)
Also see further working examples under: https://github.com/brad-jones/goerr/tree/v2/examples
# Packages
No description provided by the author
# Functions
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
Cause will unwrap the entire error chain until the root error is found.
Check will panic if err is not nill.
Handle will recover, cast the result into an error and then call the provided onError handler.
Is reports whether any error in err's chain matches target.
New is the constructor for the `Error` object.
NewStackFrame populates a stack frame object from the program counter.
NewStackTrace is the constructor for StackTrace.
PrintTrace will print the stack trace for the given error to STDERR.
Trace will take any value, convert it into an Error object, if not already one and then save the stack trace information.
Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error.
Wrap is simply a shortcut for Trace(0, err, "some message").
# Structs
Error is an error object that stores stack frame information, create new instances with `New()`.
A StackFrame contains all necessary information about a line in a callstack.
StackTrace is an object that represents a stack trace for a given error, create new instances with NewStackTrace.