Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/error namespace #331

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
.tests
.vscode
17 changes: 17 additions & 0 deletions Makefile
@@ -0,0 +1,17 @@
ifeq ($(OS),Windows_NT)
rmdir := rmdir /s /q
else
rmdir := rm -rf
endif

GOCMD=GOOS=$(GOOS) GOARCH=$(GOARCH) go

all: test

test: clean
mkdir -p .tests && \
${GOCMD} test -coverpkg=. -coverprofile=cover.out -outputdir=.tests ./... | tee .tests/report.test && \
${GOCMD} tool test2json -t < .tests/report.test > .tests/report.json

clean:
$(rmdir) .tests
17 changes: 12 additions & 5 deletions decode_hooks.go
Expand Up @@ -81,21 +81,28 @@ func ComposeDecodeHookFunc(fs ...DecodeHookFunc) DecodeHookFunc {
// If all hooks return an error, OrComposeDecodeHookFunc returns an error concatenating all error messages.
func OrComposeDecodeHookFunc(ff ...DecodeHookFunc) DecodeHookFunc {
return func(a, b reflect.Value) (interface{}, error) {
var allErrs string
var out interface{}
var err error

allErrs := NewDecodingErrors().SetFormatter(func(e *DecodingErrors) string {
errsStr := make([]string, len(e.errors))
for i := 0; i < len(e.errors); i++ {
errsStr[i] = e.errors[i].Error()
}
return strings.Join(errsStr, "\n")
})
for _, f := range ff {
out, err = DecodeHookExec(f, a, b)
if err != nil {
allErrs += err.Error() + "\n"
allErrs.Append(err)
continue
}

return out, nil
}

return nil, errors.New(allErrs)
if allErrs.Len() > 0 {
return nil, allErrs
}
return nil, nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion decode_hooks_test.go
Expand Up @@ -167,7 +167,7 @@ func TestOrComposeDecodeHookFunc_err(t *testing.T) {
if err == nil {
t.Fatalf("bad: should return an error")
}
if err.Error() != "f1 error\nf2 error\n" {
if err.Error() != "f1 error\nf2 error" {
t.Fatalf("bad: %s", err)
}
}
Expand Down