Skip to content

Commit

Permalink
Include stack for fx.Provide(..) error
Browse files Browse the repository at this point in the history
This changes error reporting to include a stack trace when an
`fx.Provide` fails from Dig.
  • Loading branch information
abhinav committed Nov 20, 2019
1 parent a4f518e commit 3761ec2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (app *App) provide(p provide) {
}

if err := app.container.Provide(constructor); err != nil {
app.err = err
app.err = fmt.Errorf("fx.Provide(%v) from:\n%+vFailed: %v", fxreflect.FuncName(constructor), p.Stack, err)
}
}

Expand Down
17 changes: 17 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@ func TestNewApp(t *testing.T) {
assert.Contains(t, err.Error(), "fx/app_test.go")
assert.Contains(t, err.Error(), "Failed: must provide constructor function")
})

t.Run("ErrorProviding", func(t *testing.T) {
err := NewForTest(t, Provide(42)).Err()
require.Error(t, err)

// Example:
// fx.Provide(..) from:
// go.uber.org/fx_test.TestNewApp.func8
// /.../fx/app_test.go:206
// testing.tRunner
// /.../go/1.13.3/libexec/src/testing/testing.go:909
// Failed: must provide constructor function, got 42 (type int)
assert.Contains(t, err.Error(), "fx.Provide(42) from:")
assert.Contains(t, err.Error(), "go.uber.org/fx_test.TestNewApp")
assert.Contains(t, err.Error(), "fx/app_test.go")
assert.Contains(t, err.Error(), "Failed: must provide constructor function")
})
}

type errHandlerFunc func(error)
Expand Down

0 comments on commit 3761ec2

Please sign in to comment.