diff --git a/nats.go b/nats.go index 988924d24..5edbdd3d3 100644 --- a/nats.go +++ b/nats.go @@ -2323,6 +2323,19 @@ func normalizeErr(line string) string { return s } +// natsProtoErr represents an -ERR protocol message sent by the server. +type natsProtoErr struct { + description string +} + +func (nerr *natsProtoErr) Error() string { + return fmt.Sprintf("nats: %s", nerr.description) +} + +func (nerr *natsProtoErr) Is(err error) bool { + return strings.ToLower(nerr.Error()) == err.Error() +} + // Send a connect protocol message to the server, issue user/password if // applicable. Will wait for a flush to return from the server for error // processing. @@ -2377,8 +2390,7 @@ func (nc *Conn) sendConnect() error { // in doReconnect()). nc.processAuthError(authErr) } - - return errors.New("nats: " + proto) + return &natsProtoErr{proto} } // Notify that we got an unexpected protocol. diff --git a/test/auth_test.go b/test/auth_test.go index 27559a750..32d80e5b4 100644 --- a/test/auth_test.go +++ b/test/auth_test.go @@ -14,6 +14,7 @@ package test import ( + "errors" "fmt" "strings" "sync/atomic" @@ -44,6 +45,10 @@ func TestAuth(t *testing.T) { t.Fatalf("Expected error '%v', got '%v'", nats.ErrAuthorization, err) } + if !errors.Is(err, nats.ErrAuthorization) { + t.Fatalf("Expected error '%v', got '%v'", nats.ErrAuthorization, err) + } + nc, err := nats.Connect("nats://derek:foo@127.0.0.1:8232") if err != nil { t.Fatal("Should have connected successfully with a token") diff --git a/test/cluster_test.go b/test/cluster_test.go index bbe3b9075..5f7595f98 100644 --- a/test/cluster_test.go +++ b/test/cluster_test.go @@ -14,6 +14,7 @@ package test import ( + "errors" "fmt" "math" "net" @@ -193,6 +194,10 @@ func TestAuthServers(t *testing.T) { t.Fatalf("Wrong error, wanted Auth failure, got '%s'\n", err) } + if !errors.Is(err, nats.ErrAuthorization) { + t.Fatalf("Expected error '%v', got '%v'", nats.ErrAuthorization, err) + } + // Test that we can connect to a subsequent correct server. var authServers = []string{ "nats://127.0.0.1:1222",