Skip to content

Commit

Permalink
Merge pull request #415 from nats-io/fix_err_case
Browse files Browse the repository at this point in the history
[FIXED] Maintain string case in errors
  • Loading branch information
kozlovic authored Dec 13, 2018
2 parents dd1ef15 + c54577a commit aa1eeb0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
16 changes: 9 additions & 7 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ func (nc *Conn) connectProto() (string, error) {

// normalizeErr removes the prefix -ERR, trim spaces and remove the quotes.
func normalizeErr(line string) string {
s := strings.ToLower(strings.TrimSpace(strings.TrimPrefix(line, _ERR_OP_)))
s := strings.TrimSpace(strings.TrimPrefix(line, _ERR_OP_))
s = strings.TrimLeft(strings.TrimRight(s, "'"), "'")
return s
}
Expand Down Expand Up @@ -2342,20 +2342,22 @@ func (nc *Conn) LastError() error {

// processErr processes any error messages from the server and
// sets the connection's lastError.
func (nc *Conn) processErr(e string) {
// Trim, remove quotes, convert to lower case.
e = normalizeErr(e)
func (nc *Conn) processErr(ie string) {
// Trim, remove quotes
ne := normalizeErr(ie)
// convert to lower case.
e := strings.ToLower(ne)

// FIXME(dlc) - process Slow Consumer signals special.
if e == STALE_CONNECTION {
nc.processOpErr(ErrStaleConnection)
} else if strings.HasPrefix(e, PERMISSIONS_ERR) {
nc.processPermissionsViolation(e)
nc.processPermissionsViolation(ne)
} else if strings.HasPrefix(e, AUTHORIZATION_ERR) {
nc.processAuthorizationViolation(e)
nc.processAuthorizationViolation(ne)
} else {
nc.mu.Lock()
nc.err = errors.New("nats: " + e)
nc.err = errors.New("nats: " + ne)
nc.mu.Unlock()
nc.Close()
}
Expand Down
30 changes: 12 additions & 18 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,39 +882,33 @@ func TestParserSplitMsg(t *testing.T) {
}

func TestNormalizeError(t *testing.T) {
received := "Typical Error"
expected := strings.ToLower(received)
if s := normalizeErr("-ERR '" + received + "'"); s != expected {
expected := "Typical Error"
if s := normalizeErr("-ERR '" + expected + "'"); s != expected {
t.Fatalf("Expected '%s', got '%s'", expected, s)
}

received = "Trim Surrounding Spaces"
expected = strings.ToLower(received)
if s := normalizeErr("-ERR '" + received + "' "); s != expected {
expected = "Trim Surrounding Spaces"
if s := normalizeErr("-ERR '" + expected + "' "); s != expected {
t.Fatalf("Expected '%s', got '%s'", expected, s)
}

received = "Trim Surrounding Spaces Without Quotes"
expected = strings.ToLower(received)
if s := normalizeErr("-ERR " + received + " "); s != expected {
expected = "Trim Surrounding Spaces Without Quotes"
if s := normalizeErr("-ERR " + expected + " "); s != expected {
t.Fatalf("Expected '%s', got '%s'", expected, s)
}

received = "Error Without Quotes"
expected = strings.ToLower(received)
if s := normalizeErr("-ERR " + received); s != expected {
expected = "Error Without Quotes"
if s := normalizeErr("-ERR " + expected); s != expected {
t.Fatalf("Expected '%s', got '%s'", expected, s)
}

received = "Error With Quote Only On Left"
expected = strings.ToLower(received)
if s := normalizeErr("-ERR '" + received); s != expected {
expected = "Error With Quote Only On Left"
if s := normalizeErr("-ERR '" + expected); s != expected {
t.Fatalf("Expected '%s', got '%s'", expected, s)
}

received = "Error With Quote Only On Right"
expected = strings.ToLower(received)
if s := normalizeErr("-ERR " + received + "'"); s != expected {
expected = "Error With Quote Only On Right"
if s := normalizeErr("-ERR " + expected + "'"); s != expected {
t.Fatalf("Expected '%s', got '%s'", expected, s)
}
}
Expand Down
20 changes: 13 additions & 7 deletions test/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAuth(t *testing.T) {

// This test may be a bit too strict for the future, but for now makes
// sure that we correctly process the -ERR content on connect.
if err.Error() != nats.ErrAuthorization.Error() {
if strings.ToLower(err.Error()) != nats.ErrAuthorization.Error() {
t.Fatalf("Expected error '%v', got '%v'", nats.ErrAuthorization, err)
}

Expand Down Expand Up @@ -304,8 +304,8 @@ func TestPermViolation(t *testing.T) {
Username: "ivan",
Password: "pwd",
Permissions: &server.Permissions{
Publish: &server.SubjectPermission{Allow: []string{"foo"}},
Subscribe: &server.SubjectPermission{Allow: []string{"bar"}},
Publish: &server.SubjectPermission{Allow: []string{"Foo"}},
Subscribe: &server.SubjectPermission{Allow: []string{"Bar"}},
},
},
}
Expand All @@ -325,20 +325,26 @@ func TestPermViolation(t *testing.T) {
defer nc.Close()

// Cause a publish error
nc.Publish("bar", []byte("fail"))
nc.Publish("Bar", []byte("fail"))
// Cause a subscribe error
nc.Subscribe("foo", func(_ *nats.Msg) {})
nc.Subscribe("Foo", func(_ *nats.Msg) {})

expectedErrorTypes := []string{"publish", "subscription"}
for _, expectedErr := range expectedErrorTypes {
select {
case e := <-errCh:
if !strings.Contains(e.Error(), nats.PERMISSIONS_ERR) {
if !strings.Contains(strings.ToLower(e.Error()), nats.PERMISSIONS_ERR) {
t.Fatalf("Did not receive error about permissions")
}
if !strings.Contains(e.Error(), expectedErr) {
if !strings.Contains(strings.ToLower(e.Error()), expectedErr) {
t.Fatalf("Did not receive error about %q, got %v", expectedErr, e.Error())
}
// Make sure subject is not converted to lower case
if expectedErr == "publish" && !strings.Contains(e.Error(), "Bar") {
t.Fatalf("Subject Bar not found in error: %v", e)
} else if expectedErr == "subscribe" && !strings.Contains(e.Error(), "Foo") {
t.Fatalf("Subject Foo not found in error: %v", e)
}
case <-time.After(2 * time.Second):
t.Fatalf("Did not get the permission error")
}
Expand Down
2 changes: 1 addition & 1 deletion test/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestAuthServers(t *testing.T) {
t.Fatalf("Expect Auth failure, got no error\n")
}

if !strings.Contains(err.Error(), "authorization") {
if !strings.Contains(err.Error(), "Authorization") {
t.Fatalf("Wrong error, wanted Auth failure, got '%s'\n", err)
}

Expand Down
2 changes: 1 addition & 1 deletion test/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ func TestServerErrorClosesConnection(t *testing.T) {

// Check LastError(), it should be "nats: <server error in lower case>"
lastErr := nc.LastError().Error()
expectedErr := "nats: " + strings.ToLower(serverSentError)
expectedErr := "nats: " + serverSentError
if lastErr != expectedErr {
t.Fatalf("Expected error: '%v', got '%v'", expectedErr, lastErr)
}
Expand Down

0 comments on commit aa1eeb0

Please sign in to comment.