Skip to content

Commit

Permalink
Merge pull request #5 from go-pogo/develop
Browse files Browse the repository at this point in the history
v0.7.2
  • Loading branch information
roeldev authored Mar 29, 2022
2 parents 29d0a74 + 2c5d579 commit a6d1ebe
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.17.x, 1.16.x, 1.15.x, 1.14.x, 1.13.x]
go-version: [1.18.x, 1.17.x, 1.16.x, 1.15.x, 1.14.x, 1.13.x]
platform: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.platform }}
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ EXAMPLE4=4_custom_error
EXAMPLE5=5_multi_error
EXAMPLE6=6_catch_panic

example%:
x%:
go run -race ./.examples/$(EXAMPLE$(*))/main.go

vet:
# go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment
# go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
go vet -vettool=$(shell where fieldalignment) ./...
25 changes: 17 additions & 8 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package errors

import (
"fmt"
"os"
"reflect"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -42,18 +43,19 @@ func New(msg interface{}) error {
return nil
}

var parent error
switch v := msg.(type) {
case *commonError:
return v

case string:
parent = Msg(v)
return newCommonErr(Msg(v), true)
case *string:
parent = Msg(*v)
return newCommonErr(Msg(*v), true)

case Msg:
parent = v
return newCommonErr(v, true)
case *Msg:
parent = *v
return newCommonErr(*v, true)

case error:
panic(panicUseWithStackInstead)
Expand All @@ -64,8 +66,6 @@ func New(msg interface{}) error {
Type: reflect.TypeOf(v).String(),
})
}

return newCommonErr(parent, true)
}

// Newf formats an error message according to a format specifier and provided
Expand Down Expand Up @@ -189,5 +189,14 @@ type UnsupportedTypeError struct {
}

func (ut *UnsupportedTypeError) Error() string {
return ut.Func + ": unsupported type `" + ut.Type + "`"
return fmt.Sprintf("%s: unsupported type `%s`", ut.Func, ut.Type)
}

// FatalOnErr prints the error to stderr and exits the program with an exit
// code that is not 0. When err is an ExitCoder its exit code is used.
func FatalOnErr(err error) {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\nFatal error: %+v\n", err)
os.Exit(GetExitCodeOr(err, 1))
}
}
6 changes: 5 additions & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ func TestNew(t *testing.T) {
})
}

t.Run("with error", func(t *testing.T) {
t.Run("with std error", func(t *testing.T) {
assert.PanicsWithValue(t, panicUseWithStackInstead, func() {
_ = New(stderrors.New(str))
})
})
t.Run("with error", func(t *testing.T) {
err := New("some err")
assert.Same(t, err, New(err))
})

tests = map[string]interface{}{
"int": 10,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.1
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
6 changes: 0 additions & 6 deletions kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ const UnknownKind Kind = ""
// It is recommended to define each Kind as a constant.
type Kind string

// Kindf formats according to a format specifier using fmt.Sprintf, and returns
// the resulting string as Kind.
func Kindf(format string, args ...interface{}) Kind {
return Kind(fmt.Sprintf(format, args...))
}

func (k Kind) Is(target error) bool {
switch t := target.(type) {
case Kind:
Expand Down
13 changes: 0 additions & 13 deletions kind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ func TestKind(t *testing.T) {
assert.Equal(t, kind.String(), kind.Error())
}

func TestKindf(t *testing.T) {
tests := map[string][]interface{}{
"no args": nil,
"some %s": {"string"},
"%s %s": {"foo", "bar"},
}
for f, a := range tests {
t.Run(f, func(t *testing.T) {
assert.Equal(t, Kind(fmt.Sprintf(f, a...)), Kindf(f, a...))
})
}
}

func TestKind_Is(t *testing.T) {
t.Run("true", func(t *testing.T) {
kind := Kind("foobar")
Expand Down
3 changes: 3 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (l *List) All() []error {
// Len returns the number of errors within the List.
func (l *List) Len() int { return len(l.list) }

// Empty return true when the list is empty.
func (l *List) Empty() bool { return len(l.list) > 0 }

// Append an error to the list. It guarantees only non-nil errors are added.
// It returns false when a nil error is encountered. And true when the error
// is appended to the list.
Expand Down
19 changes: 18 additions & 1 deletion multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ func combine(errors []error) error {
return newMultiErr(errors, 2)
}

const panicAppendNilPtr = "errors.Append: dest must not be a nil pointer"
const (
panicAppendNilPtr = "errors.Append: dest must not be a nil pointer"
panicAppendFuncNilPtr = "errors.AppendFunc: dest must not be a nil pointer"
panicAppendFuncNilFn = "errors.AppendFunc: fn must not be nil"
)

// Append appends multiple non-nil errors to a single multi error dest.
// When the value of dest is nil and errs only contains a single error, its
// value is set to the value of dest.
//
// Important: when using Append with defer, the pointer to the dest error
// must be a named return variable. For additional details see
Expand Down Expand Up @@ -85,6 +91,17 @@ func Append(dest *error, errs ...error) {
}
}

// AppendFunc appends the non-nil error result of fn to dest using Append.
func AppendFunc(dest *error, fn func() error) {
if dest == nil {
panic(panicAppendFuncNilPtr)
}
if fn == nil {
panic(panicAppendFuncNilFn)
}
Append(dest, fn())
}

type multiErr struct {
stack *StackTrace
errors []error
Expand Down
21 changes: 21 additions & 0 deletions multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ func TestAppend(t *testing.T) {
})
}

func TestAppendFunc(t *testing.T) {
t.Run("panic on nil dest ptr", func(t *testing.T) {
assert.PanicsWithValue(t, panicAppendFuncNilPtr, func() {
AppendFunc(nil, func() error { return New("bar") })
})
})
t.Run("panic on nil func", func(t *testing.T) {
assert.PanicsWithValue(t, panicAppendFuncNilFn, func() {
var err error
AppendFunc(&err, nil)
})
})
t.Run("with error", func(t *testing.T) {
var have error
want := stderrors.New("foobar")
AppendFunc(&have, func() error { return want })

assert.Same(t, want, have)
})
}

func TestMultiErr_Is(t *testing.T) {
disableTraceStack()
defer enableTraceStack()
Expand Down
14 changes: 7 additions & 7 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ func WithStack(err error) StackTracer {
return nil
}

switch err := err.(type) {
switch v := err.(type) {
case StackTracer:
return err
return v

case Msg, *Msg:
panic(panicUseNewInstead)

default:
e := &embedError{error: err}
ee := &embedError{error: v}
if traceStack {
e.stack = newStackTrace(1)
if u := Unwrap(err); u != nil {
skipStackTrace(u, e.stack.Len())
ee.stack = newStackTrace(1)
if u := Unwrap(v); u != nil {
skipStackTrace(u, ee.stack.Len())
}
}
return e
return ee
}
}

Expand Down

0 comments on commit a6d1ebe

Please sign in to comment.