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

*: replace erroneous uses of Newf/Errorf by Wrap/Wrapf #68707

Merged
merged 1 commit into from
Aug 12, 2021

Conversation

knz
Copy link
Contributor

@knz knz commented Aug 11, 2021

This is an omnibus PR touching code by multiple teams. To be reviewed as follows:

  • @adityamaru for the trace dumper changes
  • @jeffswenson @cockroachdb/sqlproxy-prs for the sql proxy code
  • @jbowens @cockroachdb/storage for the pebble changes
  • @mberhault @cockroachdb/storage for the store encryption code
  • @miretskiy for the doctor changes
  • @nvanbenschoten @cockroachdb/kv for the KV changes
  • @otan @cockroachdb/sql-experience for the GEO changes
  • @postamar @cockroachdb/sql-schema for the SQL catalog, index GC and SQL liveness changes
  • @cockroachdb/server-prs for the changes to the "security" and "server" packages

See the description below for an explanation.


When constructing an error from another error, it is generally
erroneous to use Errorf("...%v", err): this deconstructs the
original error back to a simple string and loses all its details.

It also makes the original error opaque to redactability, to be
considered an unsafe string, even if the original error already
contained a mix of safe and unsafe strings.

The proper approach is to use errors.Wrap / errors.Wrapf.
This preserves any embedded details inside the error object and its
redactability attributes.

This change was obtained by running the following command and fixing
up the results (i.e. test code was ignored):

git grep -E '(errors|fmt)\.(Errorf|Newf).*%v.*[eE]rr'|grep -v _test.go

Release note: None

@knz knz requested review from a team as code owners August 11, 2021 12:28
@knz knz requested a review from a team August 11, 2021 12:28
@knz knz requested review from a team as code owners August 11, 2021 12:28
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz
Copy link
Contributor Author

knz commented Aug 11, 2021

Arguably we'd need a linter for this.
Since preserving details in errors is distinctly an observability objective, I would suggest @dhartunian perhaps you could add a linter task to the backlog of your team?

@otan
Copy link
Contributor

otan commented Aug 11, 2021

geo lgtm

Copy link
Contributor

@mberhault mberhault left a comment

Choose a reason for hiding this comment

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

Reviewed 18 of 22 files at r1, 4 of 4 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)


pkg/ccl/cmdccl/enc_utils/main.go, line 205 at r2 (raw file):

	cipher, err := aes.NewCipher(key.rawKey)
	if err != nil {
		return nil, errors.Wrapf("could not build AES cipher for file %s", absPath)

err is not being wrapped.


pkg/cli/doctor.go, line 389 at r2 (raw file):

				id = int(descpb.InvalidID)
			} else {
				return errors.Errorf(err, "failed to parse id %s", fields[3])

This is still the original Errorf but with Wrapf arguments.


pkg/kv/kvserver/store_split.go, line 338 at r2 (raw file):

		leftRepl.writeStats.splitRequestCounts(rightRepl.writeStats)
		if err := s.addReplicaInternalLocked(rightRepl); err != nil {
			return errors.Wrapf(err, "unable to add replica %v: %s", rightRepl)

The %s for the err is still in the argument list.

Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz and @mberhault)


pkg/ccl/cmdccl/enc_utils/main.go, line 205 at r2 (raw file):

Previously, mberhault (marc) wrote…

err is not being wrapped.

Good catch. Fixed.


pkg/cli/doctor.go, line 389 at r2 (raw file):

Previously, mberhault (marc) wrote…

This is still the original Errorf but with Wrapf arguments.

Thanks fixed.

Copy link
Contributor Author

@knz knz left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mberhault)


pkg/kv/kvserver/store_split.go, line 338 at r2 (raw file):

Previously, mberhault (marc) wrote…

The %s for the err is still in the argument list.

Fixed

Copy link
Contributor

@mberhault mberhault left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mberhault)

Copy link
Contributor

@mberhault mberhault left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @knz)

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

schema :lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mberhault)

Copy link
Member

@nvanbenschoten nvanbenschoten left a comment

Choose a reason for hiding this comment

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

kv and storage :lgtm:

Reviewed 3 of 22 files at r1, 1 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mberhault)

Copy link
Contributor

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

doctor says :lgtm:

Reviewable status: :shipit: complete! 3 of 0 LGTMs obtained (waiting on @mberhault)

Copy link
Contributor

@mberhault mberhault left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: :shipit: complete! 3 of 0 LGTMs obtained (waiting on @knz)

@@ -1155,7 +1155,7 @@ func sendSnapshot(
// completed (such as defers that might be run after the previous message was
// received).
if unexpectedResp, err := stream.Recv(); err != io.EOF {
return errors.Errorf("%s: expected EOF, got resp=%v err=%v", to, unexpectedResp, err)
return errors.Wrapf(err, "%s: expected EOF, got resp=%v", to, unexpectedResp)
Copy link
Collaborator

@stevendanna stevendanna Aug 12, 2021

Choose a reason for hiding this comment

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

I haven't looked at the possible returns of stream.Recv(); but this might be problematic if err == nil since errors.Wrapf will return nil in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woah, good catch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@knz
Copy link
Contributor Author

knz commented Aug 12, 2021

@jeffswenson still waiting for your look at the proxy changes

Copy link
Collaborator

@jaylim-crl jaylim-crl left a comment

Choose a reason for hiding this comment

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

sqlproxyccl changes :lgtm: cc: @cockroachdb/sqlproxy-prs

Reviewed 1 of 1 files at r5, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 3 stale) (waiting on @stevendanna)

@knz
Copy link
Contributor Author

knz commented Aug 12, 2021

bors r=mberhault,adityamaru,miretskiy,stevendanna,jaylim-crl,ajwerner,nvanbenschoten

@craig
Copy link
Contributor

craig bot commented Aug 12, 2021

Build failed:

@knz
Copy link
Contributor Author

knz commented Aug 12, 2021

flake on #68795

When constructing an error from another error, it is generally
erroneous to use `Errorf("...%v", err)`: this deconstructs the
original error back to a simple string and loses all its details.

It also makes the original error opaque to redactability, to be
considered an unsafe string, even if the original error already
contained a mix of safe and unsafe strings.

The proper approach is to use `errors.Wrap` / `errors.Wrapf`.
This preserves any embedded details inside the error object and its
redactability attributes.

This change was obtained by running the following command and fixing
up the results (i.e. test code was ignored):

```
git grep -E '(errors|fmt)\.(Errorf|Newf).*%v.*[eE]rr'|grep -v _test.go
```

Release note: None
@knz
Copy link
Contributor Author

knz commented Aug 12, 2021

bors r=mberhault,adityamaru,miretskiy,stevendanna,jaylim-crl,ajwerner,nvanbenschoten

@craig
Copy link
Contributor

craig bot commented Aug 12, 2021

Build succeeded:

@craig craig bot merged commit 044a0d8 into cockroachdb:master Aug 12, 2021
@knz knz deleted the 20210811-errors branch August 16, 2021 13:47
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.

10 participants