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

Recover from panics that happen during fetch #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion example/pkgname/userloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion example/slice/usersliceloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions example/user_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package example

import (
"errors"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -29,6 +30,8 @@ func TestUserLoader(t *testing.T) {
for i, key := range keys {
if strings.HasPrefix(key, "E") {
errors[i] = fmt.Errorf("user not found")
} else if strings.HasPrefix(key, "P") {
panic("something bad happened")
} else {
users[i] = &User{ID: key, Name: "user " + key}
}
Expand Down Expand Up @@ -193,4 +196,21 @@ func TestUserLoader(t *testing.T) {
require.Error(t, err2[1])
require.Equal(t, "user U6", users2[0].Name)
})

t.Run("fetch panic with recover func", func(t *testing.T) {
expectedErr := errors.New("transformed")
dl.recover = func(interface{}) error {
return expectedErr
}
u, err := dl.Load("P1")
require.Nil(t, u)
require.Equal(t, err, expectedErr)
dl.recover = nil
})

t.Run("fetch panic with no recover func", func(t *testing.T) {
u, err := dl.Load("P1")
require.Nil(t, u)
require.Error(t, err)
})
}
20 changes: 19 additions & 1 deletion example/userloader_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions pkg/generator/template.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.