diff --git a/common/db/db.go b/common/db/db.go index 8d57c4af3..f9b7cce73 100644 --- a/common/db/db.go +++ b/common/db/db.go @@ -123,7 +123,7 @@ func NewSessionProvider(opts options.ToolOptions) (*SessionProvider, error) { } err = client.Ping(context.Background(), nil) if err != nil { - return nil, fmt.Errorf("failed to connect to %s: %v", opts.URI.ParsedConnString(), err) + return nil, fmt.Errorf("failed to connect to server (opts: %+v): %v", opts, err) } // create the provider diff --git a/common/db/db_test.go b/common/db/db_test.go index 481c3bee0..b053a8428 100644 --- a/common/db/db_test.go +++ b/common/db/db_test.go @@ -77,6 +77,20 @@ func TestNewSessionProvider(t *testing.T) { auth := DBGetAuthOptions() ssl := DBGetSSLOptions() Convey("When initializing a session provider", t, func() { + Convey( + "the error should return when there’s only a host", + func() { + opts := options.ToolOptions{ + Connection: &options.Connection{ + Host: "localhost:12345", + }, + SSL: &ssl, + } + + _, err := NewSessionProvider(opts) + So(err, ShouldNotBeNil) + }, + ) Convey("with the standard options, a provider with a standard"+ " connector should be returned", func() { @@ -110,7 +124,6 @@ func TestNewSessionProvider(t *testing.T) { So(err, ShouldBeNil) So(provider.client.Ping(context.Background(), nil), ShouldBeNil) }) - }) } diff --git a/common/options/options.go b/common/options/options.go index d99cc18a8..c8e8fc1ca 100644 --- a/common/options/options.go +++ b/common/options/options.go @@ -696,7 +696,7 @@ func (opts *ToolOptions) NormalizeOptionsAndURI() error { } // finalize auth options, filling in missing passwords - if opts.Auth.ShouldAskForPassword() { + if opts.Auth != nil && opts.Auth.ShouldAskForPassword() { pass, err := password.Prompt("mongo user") if err != nil { return fmt.Errorf("error reading password: %v", err)