- Formatted error stack traces
- Supports JSON marshaling
- No dependencies outside the standard library
package main
import (
"fmt"
"os"
"github.com/gofor-little/xerror"
)
func main() {
if err := RunApplication(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("application successfully started")
}
func RunApplication() error {
if err := Initialize(); err != nil {
return xerror.Wrap("failed to run application", err)
}
return nil
}
func Initialize() error {
if err := LoadConfig(); err != nil {
return xerror.Wrap("failed to initialize application", err)
}
return nil
}
func LoadConfig() error {
_, err := os.Open("config.json")
return xerror.Wrap("failed to load config", err)
}
Running the above will output...
main.RunApplication
/home/ubuntu/xerror/main.go:21: failed to run application
main.Initialize
/home/ubuntu/xerror/main.go:29: failed to initialize application
main.LoadConfig
/home/ubuntu/xerror/main.go:37: failed to load config: open config.json: no such file or directory
exit status 1
Or can be marshaled into JSON and output...
{
"error": {
"error": {
"error": "open config.json: no such file or directory",
"functionName": "main.LoadConfig",
"fileName": "/home/ubuntu/xerror/cmd/main.go",
"lineNumber": "39",
"message": "failed to load config"
},
"functionName": "main.Initialize",
"fileName": "/home/ubuntu/xerror/cmd/main.go",
"lineNumber": "31",
"message": "failed to initialize application"
},
"functionName": "main.RunApplication",
"fileName": "/home/ubuntu/xerror/cmd/main.go",
"lineNumber": "23",
"message": "failed to run application"
}
Run go test -v ./...
in the root directory.