Skip to content

Commit

Permalink
gitbase: add a more descriptive error connecting to bblfsh
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Molina <[email protected]>
  • Loading branch information
erizocosmico committed Jul 30, 2018
1 parent 0e7e98a commit 7c4fda1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 16 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (s *Session) BblfshClient() (*BblfshClient, error) {
defer s.bblfshMu.Unlock()

if s.bblfshClient == nil {
client, err := bblfsh.NewClient(s.bblfshEndpoint)
client, err := connectToBblfsh(s.bblfshEndpoint)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,7 +172,7 @@ func (s *Session) BblfshClient() (*BblfshClient, error) {

logrus.Debug("bblfsh connection is closed, opening a new one")

client, err := bblfsh.NewClient(s.bblfshEndpoint)
client, err := connectToBblfsh(s.bblfshEndpoint)
if err != nil {
return nil, err
}
Expand All @@ -196,6 +196,19 @@ func (s *Session) Close() error {
return nil
}

func connectToBblfsh(endpoint string) (*bblfsh.Client, error) {
client, err := bblfsh.NewClient(endpoint)
if err != nil {
if err == context.DeadlineExceeded {
return nil, ErrBblfshConnection.New()
}

return nil, err
}

return client, nil
}

// NewSessionBuilder creates a SessionBuilder with the given Repository Pool.
func NewSessionBuilder(pool *RepositoryPool, opts ...SessionOption) server.SessionBuilder {
return func(_ *mysql.Conn) sql.Session {
Expand All @@ -215,4 +228,4 @@ var ErrInvalidGitbaseSession = errors.NewKind("expecting gitbase session, but re
var ErrInvalidContext = errors.NewKind("invalid context received: %v")

// ErrBblfshConnection is returned when it's impossible to connect to bblfsh.
var ErrBblfshConnection = errors.NewKind("unable to establish a new bblfsh connection")
var ErrBblfshConnection = errors.NewKind("unable to establish a connection with the bblfsh server")
9 changes: 9 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ func TestSessionBblfshClient(t *testing.T) {
require.NotNil(cli)
require.Equal(connectivity.Ready, cli.GetState())
}

func TestSessionBblfshClientNoConnection(t *testing.T) {
require := require.New(t)

session := NewSession(nil, WithBblfshEndpoint("localhost:9999"))
_, err := session.BblfshClient()
require.Error(err)
require.True(ErrBblfshConnection.Is(err))
}

0 comments on commit 7c4fda1

Please sign in to comment.