Getting Started - Error Handling and Manipulation
What is Grace?
Grace is an excellent partner in the management of errors and fatal events: it can be used in every kind of project with ease. Grace goal is to provide a general purpose tool based on the standard Go error structure that can help developers without a big effort on the integration. Save lines of code and testing without reinventing the wheel every time.
It supplies many methods to compare and works with errors, a list of handlers for transforming the output and an easy way to catch panic errors. Grace also support multiple errors in a single error, returning a list of errors as a single error type. These multiple errors can be used as a standard error because it works as multiple error only with Grace methods.
QuickStart
The package is available on github and can be installed by:
go get -u github.com/oxequa/grace
The following is a simple example that handles a panic and returns the error without the program crash.
package main
import (
"fmt"
"github.com/oxequa/grace"
)
func example() (e error){
defer grace.Recover(&e) // save recover error and stack trace to e
numbers := []int{1, 2}
fmt.Println(numbers[3]) // panic out of index
return
}
func main() {
err := example() // no panic occur
fmt.Println(err)
fmt.Println("End")
}
Requirements
Grace is developed on Go 1.10 and it is tested on Mac OS X (last version), Linux Ubuntu 16.04 and Windows 10. Operation on previous versions is not guaranteed but should be supported without problems since Go 1.7.x.