These steps cover the writing and running of your application:
- From your Terminal/console application, create a new directory called ~/projects/go-programming-cookbook/chapter4/errwrap and navigate to this directory.
- Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter4/errwrap
You should see a file called go.mod that contains the following:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter4/errwrap
- Copy tests from ~/projects/go-programming-cookbook-original/chapter4/errwrap, or use this as an exercise to write some of your own code!
- Create a file called errwrap.go with the following content:
package errwrap
import (
"fmt"
"github.com/pkg/errors"
)
// WrappedError demonstrates error wrapping and
// annotating an error
func WrappedError(e error) error {
return errors.Wrap(e, "An error occurred in WrappedError")
}
// ErrorTyped is a error we can check against
type ErrorTyped struct{
error
}
// Wrap shows what happens when we wrap an error
func Wrap() {
e := errors.New("standard error")
fmt.Println("Regular Error - ", WrappedError(e))
fmt.Println("Typed Error - ",
WrappedError(ErrorTyped{errors.New("typed error")}))
fmt.Println("Nil -", WrappedError(nil))
}
- Create a file called unwrap.go with the following content:
package errwrap
import (
"fmt"
"github.com/pkg/errors"
)
// Unwrap will unwrap an error and do
// type assertion to it
func Unwrap() {
err := error(ErrorTyped{errors.New("an error occurred")})
err = errors.Wrap(err, "wrapped")
fmt.Println("wrapped error: ", err)
// we can handle many error types
switch errors.Cause(err).(type) {
case ErrorTyped:
fmt.Println("a typed error occurred: ", err)
default:
fmt.Println("an unknown error occurred")
}
}
// StackTrace will print all the stack for
// the error
func StackTrace() {
err := error(ErrorTyped{errors.New("an error occurred")})
err = errors.Wrap(err, "wrapped")
fmt.Printf("%+v\n", err)
}
- Create a new directory named example and navigate to it.
- Create a main.go file with the following content:
package main
import (
"fmt"
"github.com/PacktPublishing/
Go-Programming-Cookbook-Second-Edition/
chapter4/errwrap"
)
func main() {
errwrap.Wrap()
fmt.Println()
errwrap.Unwrap()
fmt.Println()
errwrap.StackTrace()
}
- Run go run main.go.
- You may also run the following commands:
$ go build
$ ./example
You should now see the following output:
$ go run main.go
Regular Error - An error occurred in WrappedError: standard
error
Typed Error - An error occurred in WrappedError: typed error
Nil - <nil>
wrapped error: wrapped: an error occurred
a typed error occurred: wrapped: an error occurred
an error occurred
github.com/PacktPublishing/Go-Programming-Cookbook-Second-
Edition/chapter4/errwrap.StackTrace
/Users/lothamer/go/src/github.com/agtorre/go-
cookbook/chapter4/errwrap/unwrap.go:30
main.main
/tmp/go/src/github.com/agtorre/go-
cookbook/chapter4/errwrap/example/main.go:14
- The go.mod file should be updated and the go.sum file should now be present in the top-level recipe directory.
- If you copied or wrote your own tests, then go up one directory and run go test. Ensure that all the tests pass.