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

Fix false-positives for github.com/pkg/errors functions #15

Merged
merged 1 commit into from
Jun 15, 2021

Conversation

r2k1
Copy link
Contributor

@r2k1 r2k1 commented Jun 14, 2021

Add support for errors.WithMessagef( and errors.WithStack(

Wrapcheck complains about the next code:

package store

import (
	"database/sql"
	stderrors "errors"

	"github.com/pkg/errors"
)

var ErrNoRowsAffected = stderrors.New("no rows affected")
var ErrMultipleRowsAffected = stderrors.New("multiple rows affected")

// EnsureOneRowAffected ensures that a single row was affected by db.ExecWithResult.
func EnsureOneRowAffected(res sql.Result, err error) error {
	if err != nil {
		return err
	}

	rows, err := res.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "could not fetch information about affected rows")
	}

	if rows == 0 {
		return errors.WithStack(ErrNoRowsAffected)
	}

	if rows > 1 {
		return errors.WithStack(ErrMultipleRowsAffected)
	}

	return nil
}
error returned from external package is unwrapped: sig: func github.com/pkg/errors.WithStack(err error) error (wrapcheck)
                return errors.WithStack(ErrNoRowsAffected)

@r2k1
Copy link
Contributor Author

r2k1 commented Jun 14, 2021

How do you test it?

I got the next error when trying to run it:

=== RUN   TestAnalyzer/ignore_pkg_errors
main.go:6:2: cannot find package "github.com/pkg/errors" in any of:
	/usr/local/go/src/github.com/pkg/errors (from $GOROOT)
	/Users/r2k1/projects/wrapcheck/wrapcheck/testdata/ignore_pkg_errors/src/github.com/pkg/errors (from $GOPATH)
/Users/r2k1/projects/wrapcheck/wrapcheck/testdata/ignore_pkg_errors/main.go:6:2: could not import github.com/pkg/errors (invalid package name: "")
    analysistest.go:274: error analyzing wrapcheck@_/Users/r2k1/projects/wrapcheck/wrapcheck/testdata/ignore_pkg_errors: analysis skipped due to errors in package
    --- FAIL: TestAnalyzer/ignore_pkg_errors (1.16s)

@tomarrell tomarrell force-pushed the ignore-with-stack branch from 7829f22 to a0a6e37 Compare June 15, 2021 20:24
@tomarrell
Copy link
Owner

Hey man! Long time no chat, how're things?

Yeah the test issue was a limitation of analysistest not yet supporting go mod unfortunately, golang/go#37054. Have now manually vendored and resolved it on master, then rebased this PR.

PR looks good, thanks for the contribution! 😃

@tomarrell tomarrell merged commit 56ecd1e into tomarrell:master Jun 15, 2021
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.

2 participants