Skip to content

Commit 54f28f2

Browse files
authored
Merge pull request #407 from erizocosmico/enhancement/bblfsh-conn-error
gitbase: add a more descriptive error connecting to bblfsh
2 parents dad97d0 + 7c4fda1 commit 54f28f2

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

session.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (s *Session) BblfshClient() (*BblfshClient, error) {
143143
defer s.bblfshMu.Unlock()
144144

145145
if s.bblfshClient == nil {
146-
client, err := bblfsh.NewClient(s.bblfshEndpoint)
146+
client, err := connectToBblfsh(s.bblfshEndpoint)
147147
if err != nil {
148148
return nil, err
149149
}
@@ -172,7 +172,7 @@ func (s *Session) BblfshClient() (*BblfshClient, error) {
172172

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

175-
client, err := bblfsh.NewClient(s.bblfshEndpoint)
175+
client, err := connectToBblfsh(s.bblfshEndpoint)
176176
if err != nil {
177177
return nil, err
178178
}
@@ -196,6 +196,19 @@ func (s *Session) Close() error {
196196
return nil
197197
}
198198

199+
func connectToBblfsh(endpoint string) (*bblfsh.Client, error) {
200+
client, err := bblfsh.NewClient(endpoint)
201+
if err != nil {
202+
if err == context.DeadlineExceeded {
203+
return nil, ErrBblfshConnection.New()
204+
}
205+
206+
return nil, err
207+
}
208+
209+
return client, nil
210+
}
211+
199212
// NewSessionBuilder creates a SessionBuilder with the given Repository Pool.
200213
func NewSessionBuilder(pool *RepositoryPool, opts ...SessionOption) server.SessionBuilder {
201214
return func(_ *mysql.Conn) sql.Session {
@@ -215,4 +228,4 @@ var ErrInvalidGitbaseSession = errors.NewKind("expecting gitbase session, but re
215228
var ErrInvalidContext = errors.NewKind("invalid context received: %v")
216229

217230
// ErrBblfshConnection is returned when it's impossible to connect to bblfsh.
218-
var ErrBblfshConnection = errors.NewKind("unable to establish a new bblfsh connection")
231+
var ErrBblfshConnection = errors.NewKind("unable to establish a connection with the bblfsh server")

session_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ func TestSessionBblfshClient(t *testing.T) {
1616
require.NotNil(cli)
1717
require.Equal(connectivity.Ready, cli.GetState())
1818
}
19+
20+
func TestSessionBblfshClientNoConnection(t *testing.T) {
21+
require := require.New(t)
22+
23+
session := NewSession(nil, WithBblfshEndpoint("localhost:9999"))
24+
_, err := session.BblfshClient()
25+
require.Error(err)
26+
require.True(ErrBblfshConnection.Is(err))
27+
}

0 commit comments

Comments
 (0)