Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add natsProtoErr that can be used with errors.Is #1082

Merged
merged 1 commit into from Sep 15, 2022
Merged

Conversation

wallyqs
Copy link
Member

@wallyqs wallyqs commented Sep 14, 2022

The server can send both -ERR 'Authorization Violation' or -ERR 'authorization violation' depending on some conditions so in the client side we usually try to normalize the behavior by changing to lowercase when checking for an error.
One exception was during the initial connect where the client instead returned the raw error protocol message.

Since this behavior has been in the wild for many years, instead of returning the raw error or the proper error type like ErrAuthorization to keep things backwards compatible just in case, this adds a new natsProtoErr that can be used with errors.Is to check whether the raw protocol message is equivalent to other errors.

Fixes #1081

Signed-off-by: Waldemar Quevedo wally@nats.io

Signed-off-by: Waldemar Quevedo <wally@nats.io>
@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 85.578% when pulling 275563c on auth-error-is into 25b6392 on main.

@@ -2377,8 +2390,7 @@ func (nc *Conn) sendConnect() error {
// in doReconnect()).
nc.processAuthError(authErr)
}

return errors.New("nats: " + proto)
return &natsProtoErr{proto}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but have you tried adding a return authErr in the "if" statement above? Does that work? Point is that we may not need to do any conversions for the known errors handled in checkAuthError. Your changes is still valid I guess for all other cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And by "does it work", I mean the user code could then do if err == nats.ErrAuthorization without even have to use the errors.Is().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work but then I saw that we also tested for uppercase literal error in other tests like: https://github.com/nats-io/nats.go/blob/main/test/cluster_test.go#L192-L194
and recommendation from Go team is to start using errors.Is anyway instead of checking directly to check for wrapped errors as well.

Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@piotrpio piotrpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wallyqs wallyqs merged commit caf5af2 into main Sep 15, 2022
@wallyqs wallyqs deleted the auth-error-is branch September 15, 2022 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Casing issue in library prevents ErrorIs from working correctly
4 participants