Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.tests
.vscode
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading