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

Is methods should be exempt for type assertions and switches too #50

Closed
triarius opened this issue Aug 2, 2023 · 0 comments · Fixed by #51
Closed

Is methods should be exempt for type assertions and switches too #50

triarius opened this issue Aug 2, 2023 · 0 comments · Fixed by #51

Comments

@triarius
Copy link
Contributor

triarius commented Aug 2, 2023

When fixing #11, it was decided to make an exemption for comparisons and switches in Is methods. This works for errors that are atomic values, but some errors have more structure and need a more detailed comparison.

For example, for an error like

type ComplexError struct {
	code int
	message string
}

func (e *ComplexError) Error() string {
	return fmt.Sprintf(%d: %s, e.code, e.message)
}

we may want to do more than just compare types in an Is method, we may want to assert the error's codes and message match as well:

func (e *ComplexError) Is(target error) bool {
	terr, ok := target.(*ComplexError)
	return ok && *e == *terr
}

If we want to provide an equivalence between errors of different types, we may also need to use the switch form of type assertion.

type AnotherComplexError {
	code int
	message []byte
}

func (e *AnotherComplexError) Error() string {
	return fmt.Sprintf(%d: %s, e.code, e.message)
}

func (e *AnotherComplexError) Is(target error) bool {
	switch terr := target.(type) {
	case *AnotherComplexError:
		return *e == *terr
	case *ComplexError:
		return e.code == terr.code && e.message == []byte(terr.message)
	default:
		false
	}
}

with an appropriate method for *ComplexError as well.

Currently, both forms of type assertion are warned of by go-errorlint.

triarius added a commit to triarius/go-errorlint that referenced this issue Aug 2, 2023
triarius added a commit to triarius/go-errorlint that referenced this issue Aug 2, 2023
triarius added a commit to triarius/go-errorlint that referenced this issue Aug 15, 2023
triarius added a commit to triarius/go-errorlint that referenced this issue Aug 15, 2023
polyfloyd pushed a commit that referenced this issue Aug 20, 2023
polyfloyd pushed a commit that referenced this issue Aug 20, 2023
tri-adam added a commit to tri-adam/sif that referenced this issue Aug 21, 2023
As of v1.4.4, go-errorlint exempts `Is` methods from type assertions
(see polyfloyd/go-errorlint#50).
DrDaveD pushed a commit to DrDaveD/sif that referenced this issue Aug 24, 2023
As of v1.4.4, go-errorlint exempts `Is` methods from type assertions
(see polyfloyd/go-errorlint#50).

Signed-off-by: Dave Dykstra <2129743+DrDaveD@users.noreply.github.com>
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 a pull request may close this issue.

1 participant