Skip to content

Commit

Permalink
Fix panic in Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed May 27, 2017
1 parent 13204cd commit 9c4ee57
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func (c *Container) Provide(constructor interface{}) error {
// immediately.
func (c *Container) Invoke(function interface{}) error {
ftype := reflect.TypeOf(function)
if ftype == nil {
return errors.New("can't invoke an untyped nil")
}
if ftype.Kind() != reflect.Func {
return fmt.Errorf("can't invoke non-function %v (type %v)", function, ftype)
}
Expand Down
5 changes: 5 additions & 0 deletions dig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ func TestInvokesUseCachedObjects(t *testing.T) {
func TestInvokeFailures(t *testing.T) {
t.Parallel()

t.Run("untyped nil", func(t *testing.T) {
c := New()
assert.Error(t, c.Invoke(nil))
})

t.Run("unmet dependency", func(t *testing.T) {
c := New()
assert.Error(t, c.Invoke(func(*bytes.Buffer) {}))
Expand Down

0 comments on commit 9c4ee57

Please sign in to comment.