Skip to content

Commit

Permalink
[ADDED] Invoke ErrorHandler on preparing connection string when recon…
Browse files Browse the repository at this point in the history
…necting (#1397)

Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
  • Loading branch information
piotrpio committed Sep 12, 2023
1 parent 8e75bfd commit 6740b12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2501,6 +2501,9 @@ func (nc *Conn) sendConnect() error {
// Construct the CONNECT protocol string
cProto, err := nc.connectProto()
if err != nil {
if !nc.initc && nc.Opts.AsyncErrorCB != nil {
nc.ach.push(func() { nc.Opts.AsyncErrorCB(nc, nil, err) })
}
return err
}

Expand Down
32 changes: 32 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,38 @@ func TestUserCredentialsChainedFile(t *testing.T) {
}
}

func TestReconnectMissingCredentials(t *testing.T) {
ts := runTrustServer()
defer ts.Shutdown()

chainedFile := createTmpFile(t, []byte(chained))
defer os.Remove(chainedFile)

url := fmt.Sprintf("nats://127.0.0.1:%d", TEST_PORT)
errs := make(chan error, 1)
nc, err := Connect(url, UserCredentials(chainedFile), ErrorHandler(func(_ *Conn, _ *Subscription, err error) {
errs <- err
}))
if err != nil {
t.Fatalf("Expected to connect, got %v", err)
}
defer nc.Close()
os.Remove(chainedFile)
ts.Shutdown()

ts = runTrustServer()
defer ts.Shutdown()

select {
case err := <-errs:
if !strings.Contains(err.Error(), "no such file or directory") {
t.Fatalf("Expected error about missing creds file, got %q", err)
}
case <-time.After(5 * time.Second):
t.Fatal("Did not get error about missing creds file")
}
}

func TestUserJWTAndSeed(t *testing.T) {
if server.VERSION[0] == '1' {
t.Skip()
Expand Down

0 comments on commit 6740b12

Please sign in to comment.