Skip to content

brad-jones/goerr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goerr

PkgGoDev GoReport GoLang .github/workflows/main.yml semantic-release Conventional Commits KeepAChangelog License

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