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

Implement an error-comparing method to errorsx String #841

Merged
merged 1 commit into from
Dec 9, 2020

Conversation

sryoya
Copy link
Contributor

@sryoya sryoya commented Nov 6, 2020

PR preparation

What

  • implemented an error-comparing method Is() to the struct String in the package errorsx
  • create a test file for errorsx on the way to make a test for this func

Why
To make it possible to compare the custom error (String in errorsx) with all the struct implementing error interface by errors.Is.
That enables users of this package to check returned exported errors in their own applications by the latest Go standard error comparing way.

For example, the package user can handle the returned error like this

if errors.Is(err, slack.ErrRTMDisconnected) {
  // do something
}

From Go1.13, errors.Is is provided as a function of a Go standard package.
And, to make the custom error comparable by this func, we need to implement Is method to that.
From the package description of errors

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

An error type might provide an Is method so it can be treated as equivalent to an existing error. For example, if MyError defines

func (m MyError) Is(target error) bool { return target == os.ErrExist }

then Is(MyError{}, os.ErrExist) returns true. See syscall.Errno.Is for an example in the standard library.

Note
Firstly I meant and tried to update result evaluation in some test files by this func, but it was too early considering the consistency for the old Go version. So, now I propose this func just for increasing the package user's options to check the returned error from this package.

@sryoya sryoya changed the title Implement a error-comparing method to errorsx String Implement an error-comparing method to errorsx String Nov 6, 2020
Copy link
Member

@kanata2 kanata2 left a comment

Choose a reason for hiding this comment

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

👍
Could you put commits together?

Note: I'm currently considering only supporting Go 1.13+.

Refactor views test using errors.Is

introduce xerrors.Is instead of errors.Is

Revert "Refactor views test using errors.Is"

This reverts commit f09d336.
@sryoya sryoya force-pushed the implement_Is_func_to_errorsx branch from aa39d88 to c78729e Compare December 9, 2020 22:29
@sryoya
Copy link
Contributor Author

sryoya commented Dec 9, 2020

Could you put commits together?

done 👍

Note: I'm currently considering only supporting Go 1.13+.

I agree with that. I think enough time has passed since Go1.13 was released.

@sryoya sryoya requested a review from kanata2 December 9, 2020 22:34
Copy link
Member

@kanata2 kanata2 left a comment

Choose a reason for hiding this comment

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

Looks good to me :)

@kanata2 kanata2 merged commit 1ab3398 into slack-go:master Dec 9, 2020
@sryoya sryoya deleted the implement_Is_func_to_errorsx branch December 10, 2020 01:31
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.

None yet

2 participants