Sitelet https://github.com/hookdeck/hookdeck-cli/pull/377/commits/d9d092143436b960e3af38e5fa3b705855716be7
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: say why the connection failed, not just that it did
`listen` gave up with "Could not connect. Terminating after 10 failed attempts
to establish a connection." and no reason. The reason existed - the dial error
is right there in connect() - but it was logged at debug, so at the default log
level the user was told the CLI had stopped trying and nothing about whether it
was DNS, a refused connection, a proxy, or a rejected session.

That is the other half of the report this branch already addresses. The
readiness line tells you when the tunnel came up; this tells you why when it
did not. A caller who saw neither had no way to tell a healthy tunnel from a
broken one, which is exactly how a working connection got filed as a failure.

The websocket client now keeps the last connect error and the proxy appends it
to the terminating message:

  Could not connect. Terminating after 10 failed attempts to establish a
  connection. Last error: dial tcp [::1]:39999: connect: connection refused

Held back earlier as too broad for a patch release. That reasoning expired when
the patch was dropped in favour of shipping in v2.6.0.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01BnrKWZQASV7bFJ4oGWwmo9
  • Loading branch information
leggetter and claude committed Sep 10, 2026
commit d9d092143436b960e3af38e5fa3b705855716be7
7 changes: 7 additions & 0 deletions pkg/listen/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ func (p *Proxy) Run(parentCtx context.Context) error {
}
if !canConnect() {
p.renderer.Cleanup()
// Report the reason, not just the count. Without this the user is
// told the CLI gave up but not whether it was DNS, a refused
// connection, a proxy, or a rejected session — and the reason is
// only logged at debug level.
if connectErr := wsClient.LastConnectErr(); connectErr != nil {
return fmt.Errorf("Could not connect. Terminating after %d failed attempts to establish a connection. Last error: %v", nAttempts, connectErr)
}
return fmt.Errorf("Could not connect. Terminating after %d failed attempts to establish a connection.", nAttempts)
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type Client struct {
// read by Stop(), which can run on the signal-handler goroutine.
stateMu sync.Mutex

// lastConnectErr is why the most recent connect attempt failed. Guarded by
// stateMu: written by Run, read by LastConnectErr from the proxy goroutine.
lastConnectErr error

NotifyExpired chan struct{}
notifyClose chan error
send chan *OutgoingMessage
Expand Down Expand Up @@ -129,6 +133,21 @@ func (c *Client) connected() bool {
return c.isConnected
}

// LastConnectErr returns why the most recent connect attempt failed, or nil if
// none has. The reason is otherwise only visible at debug level, which left the
// CLI able to say it had given up but not why.
func (c *Client) LastConnectErr() error {
c.stateMu.Lock()
defer c.stateMu.Unlock()
return c.lastConnectErr
}

func (c *Client) setLastConnectErr(err error) {
c.stateMu.Lock()
c.lastConnectErr = err
c.stateMu.Unlock()
}

// HasConnected reports whether this client successfully established its
// websocket connection at some point. It stays true after a disconnect, so
// callers can distinguish "connected then dropped" from "never connected".
Expand All @@ -151,6 +170,7 @@ func (c *Client) Run(ctx context.Context) {

err := c.connect(ctx)
if err != nil {
c.setLastConnectErr(err)
c.cfg.Log.WithFields(log.Fields{
"prefix": "websocket.client.Run",
}).Debug(err)
Expand Down
33 changes: 33 additions & 0 deletions pkg/websocket/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,36 @@ func TestStopConcurrentWithConnect(t *testing.T) {
}()
wg.Wait()
}

// TestLastConnectErr covers the reason a connect attempt failed being readable
// by the caller. It was previously logged at debug level only, so `listen` could
// report that it had given up after ten attempts without saying whether the
// cause was DNS, a refused connection, a proxy, or a rejected session.
func TestLastConnectErr(t *testing.T) {
t.Run("nil before any attempt", func(t *testing.T) {
c := NewClient("ws://localhost:1", "cses_x", "key", "tm_x", nil, "", &Config{})
if err := c.LastConnectErr(); err != nil {
t.Fatalf("expected no error before connecting, got %v", err)
}
})

t.Run("records why the dial failed", func(t *testing.T) {
// Port 1 is reserved and nothing listens on it, so the dial fails fast.
c := NewClient("ws://127.0.0.1:1", "cses_x", "key", "tm_x", nil, "", &Config{})

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

// Run signals ConnectionLost on failure; drain it so Run can return.
go func() { <-c.NotifyExpired }()
c.Run(ctx)

err := c.LastConnectErr()
if err == nil {
t.Fatal("a failed dial must leave a reason behind")
}
if !strings.Contains(err.Error(), "connect") {
t.Errorf("expected the reason to name the transport failure, got %q", err)
}
})
}
Loading