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

Use pointer receiver for type-defined errors #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wolfogre
Copy link

The old code use value receiver for type-defined errors like ErrTaken, it makes ErrTaken and *ErrTaken are both valid errors.

When I saw the the value receiver, I thought the way to check errrors should be

var target redsync.ErrTaken
if errors.As(err, &target) {
	// ...
}

Unfortunately, it didn't work. Since the code returned &ErrTaken.

err = multierror.Append(err, &RedisError{Node: r.node, Err: r.err})


And I also see mix-use of the two types in the repo, the test code uses RedisError as an error:

redsync/error_test.go

Lines 19 to 22 in b292c9f

err := RedisError{
Node: 0,
Err: v,
}

But here it uses *RedisError as an error:

err = multierror.Append(err, &RedisError{Node: r.node, Err: r.err})


So, I believe it's be a good idea to use pointer receiver for type-defined errors, just like most errors in golang standard packages like url.Error and os.PathError. It doesn't break anything, since the right ways to handle such errors are the same before or after this PR:

var target *redsync.ErrTaken
if errors.As(err, &target) {
	// ...
}

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.

1 participant