Skip to content

Commit

Permalink
Add example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski committed Mar 20, 2023
1 parent b8049ac commit 081233a
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 33 deletions.
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,83 @@ The exported package is `errors`, basic usage:
import "github.com/coditory/go-errors"

func main() {
// TBD
err := foo()
fmt.Printf("\n>>> Format %%s:\n%s", err)
fmt.Printf("\n>>> Format %%v:\n%v", err)
fmt.Printf("\n>>> Format %%+v:\n%+v", err)
fmt.Printf("\n>>> Format %%#v:\n%#v", err)
}

func foo() error {
err := bar()
return errors.Wrap(err, "foo failed")
}

func bar() error {
return errors.New("bar failed")
}
```

Output for `go run ./samples`

```
>>> Format %s:
foo failed
>>> Format %v:
foo failed
main.foo:19
main.main:10
runtime.main:250
runtime.goexit:1598
caused by: bar failed
main.bar:23
main.foo:18
main.main:10
runtime.main:250
runtime.goexit:1598
>>> Format %+v:
foo failed
./samples.go:19
main.foo
./samples.go:10
main.main
<GO_SRC_DIR>/runtime/proc.go:250
runtime.main
<GO_SRC_DIR>/runtime/asm_amd64.s:1598
runtime.goexit
caused by: bar failed
./samples.go:23
main.bar
./samples.go:18
main.foo
./samples.go:10
main.main
<GO_SRC_DIR>/runtime/proc.go:250
runtime.main
<GO_SRC_DIR>/runtime/asm_amd64.s:1598
runtime.goexit
>>> Format %#v:
foo failed
<PROJECT_DIR>/samples/samples.go:19
main.foo
<PROJECT_DIR>/samples/samples.go:10
main.main
<GO_SRC_DIR>/runtime/proc.go:250
runtime.main
<GO_SRC_DIR>/runtime/asm_amd64.s:1598
runtime.goexit
caused by: bar failed
<PROJECT_DIR>/samples/samples.go:23
main.bar
<PROJECT_DIR>/samples/samples.go:18
main.foo
/Users/mendlik/Development/go/go-errors/samples/samples.go:10
main.main
<GO_SRC_DIR>/runtime/proc.go:250
runtime.main
<GO_SRC_DIR>/runtime/asm_amd64.s:1598
runtime.goexit
```
22 changes: 11 additions & 11 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ type Error struct {
}

func (e *Error) Error() string {
if e.message != "" && e.err != nil {
return fmt.Sprintf("%s: %s", e.message, e.err.Error())
}
if e.message != "" {
return e.message
}
Expand All @@ -71,15 +68,15 @@ func (e *Error) StackTrace() StackTrace {
func (e *Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
e.formatCauses(s, s.Flag('+'))
e.formatCauses(s)
case 's':
write(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}

func (e *Error) formatCauses(s fmt.State, plus bool) {
func (e *Error) formatCauses(s fmt.State) {
var err error
err = e
causes := 0
Expand All @@ -99,9 +96,12 @@ func (e *Error) formatCauses(s fmt.State, plus bool) {
}
for i := 0; i < n; i++ {
frame := stacktrace[i]
if plus {
if s.Flag('+') {
fmt.Fprintf(s, "\t%s:%d\n", frame, frame)
fmt.Fprintf(s, "\t\t%n\n", frame)
} else if s.Flag('#') {
fmt.Fprintf(s, "\t%+s:%d\n", frame, frame)
fmt.Fprintf(s, "\t\t%+n\n", frame)
} else {
fmt.Fprintf(s, "\t%n:%d\n", frame, frame)
}
Expand All @@ -114,10 +114,6 @@ func (e *Error) formatCauses(s fmt.State, plus bool) {
}
}
}
if werr, ok := err.(*Error); ok {
// deduplicate wrapped error message
err = werr.Unwrap()
}
if werr, ok := err.(wrapper); ok {
err = werr.Unwrap()
} else {
Expand Down Expand Up @@ -298,7 +294,11 @@ func relName(name string) string {
if strings.HasPrefix(BasePath, "**/") {
i := strings.Index(name, BasePath[3:])
if i > 0 {
return "./" + name[i+len(BasePath)-2:]
name = name[i+len(BasePath)-2:]
for strings.HasPrefix(name, BasePath[3:]) {
name = name[len(BasePath)-2:]
}
return "./" + name
}
} else if strings.HasPrefix(name, BasePath) {
return name[len(BasePath)+1:]
Expand Down
24 changes: 24 additions & 0 deletions samples/samples.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"

"github.com/coditory/go-errors"
)

func main() {
err := foo()
fmt.Printf("\n>>> Format %%s:\n%s", err)
fmt.Printf("\n>>> Format %%v:\n%v", err)
fmt.Printf("\n>>> Format %%+v:\n%+v", err)
fmt.Printf("\n>>> Format %%#v:\n%#v", err)
}

func foo() error {
err := bar()
return errors.Wrap(err, "foo failed")
}

func bar() error {
return errors.New("bar failed")
}
11 changes: 11 additions & 0 deletions test/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package errors_test

import (
"github.com/coditory/go-errors"
)

func init() {
errors.BaseModule = "github.com/coditory/go-errors"
errors.BasePath = "**/go-errors"
errors.BaseCachePath = "**/mod/pkg"
}
21 changes: 0 additions & 21 deletions test/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,12 @@ import (

type ErrorsSuite struct {
suite.Suite
BackupBasePath string
BackupBaseModule string
BackupBaseCachePath string
}

func TestErrorsSuite(t *testing.T) {
suite.Run(t, new(ErrorsSuite))
}

func (suite *ErrorsSuite) SetupSuite() {
suite.BackupBaseModule = errors.BaseModule
suite.BackupBasePath = errors.BasePath
suite.BackupBaseCachePath = errors.BaseCachePath
}

func (suite *ErrorsSuite) TearDownSuite() {
errors.BaseModule = suite.BackupBaseModule
errors.BasePath = suite.BackupBasePath
errors.BaseCachePath = suite.BackupBaseCachePath
}

func (suite *ErrorsSuite) SetupTest() {
errors.BaseModule = "github.com/coditory/go-errors"
errors.BasePath = "**/go-errors"
errors.BaseCachePath = "**/mod/pkg"
}

func (suite *ErrorsSuite) TestNewError() {
err := errors.New("foo")
suite.Equal("foo", err.Error())
Expand Down

0 comments on commit 081233a

Please sign in to comment.