Skip to content

Commit

Permalink
Handle panic in IBM initialization (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdmanv committed Sep 19, 2019
1 parent f455078 commit 4bf0e2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/blockstorage/ibm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ type client struct {

//newClient returns a Client struct
func newClient(ctx context.Context, args map[string]string) (*client, error) {
return handleClientPanic(func() (*client, error) {
return newClientUnsafe(ctx, args)
})
}

func handleClientPanic(f func() (*client, error)) (c *client, err error) {
defer func() {
r := recover()
if r == nil {
return
}
if e, ok := r.(error); ok {
err = errors.Wrap(e, "IBM client panicked during initialization")
} else {
err = errors.Errorf("IBM client panicked during initialization: %s", r)
}
}()
return f()
}

// newClientUnsafe may panic. See https://github.com/IBM/ibmcloud-storage-volume-lib/issues/79
func newClientUnsafe(ctx context.Context, args map[string]string) (*client, error) {

zaplog, _ := zap.NewProduction()
defer zaplog.Sync() // nolint: errcheck
Expand Down
24 changes: 24 additions & 0 deletions pkg/blockstorage/ibm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"path/filepath"
"testing"

ibmcfg "github.com/IBM/ibmcloud-storage-volume-lib/config"

. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -154,3 +156,25 @@ func (s *ClientSuite) getCredsMap(c *C) map[string]string {
c.Skip(fmt.Sprintf("Neither of %s, %s environment variables set", IBMApiKeyEnv, IBMSLApiKeyEnv))
return map[string]string{}
}

func (s *ClientSuite) TestPanic(c *C) {
for _, f := range []func() (*client, error){
func() (*client, error) {
panic("TEST")
},
func() (*client, error) {
var cfg *client
cfg.SLCfg = ibmcfg.SoftlayerConfig{}
return nil, nil
},
func() (*client, error) {
var x []int
x[0]++
return nil, nil
},
} {
cfg, err := handleClientPanic(f)
c.Assert(err, NotNil)
c.Assert(cfg, IsNil)
}
}

0 comments on commit 4bf0e2d

Please sign in to comment.