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

*: migrate to Go stdlib error wrapping #556

Merged
merged 9 commits into from
Nov 29, 2024

Conversation

cyphar
Copy link
Member

@cyphar cyphar commented Nov 28, 2024

Fixes #388

tych0
tych0 previously approved these changes Nov 28, 2024
@cyphar
Copy link
Member Author

cyphar commented Nov 28, 2024

Yeah, it turns out depending on the behaviour of errors.Wrap(nil, ...) was a bad idea... It might be a good idea to make our own wrapper for now to avoid too much pain, but making it a proper printf wrapper would require us to parse the format string...

@codecov-commenter
Copy link

codecov-commenter commented Nov 28, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 32.98429% with 384 lines in your changes missing coverage. Please review.

Project coverage is 72.43%. Comparing base (094e766) to head (e9fff47).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
pkg/unpriv/unpriv.go 45.03% 52 Missing and 20 partials ⚠️
oci/layer/tar_extract.go 18.33% 48 Missing and 1 partial ⚠️
oci/cas/dir/dir.go 35.08% 34 Missing and 3 partials ⚠️
oci/layer/unpack.go 20.51% 28 Missing and 3 partials ⚠️
mutate/mutate.go 9.09% 20 Missing ⚠️
oci/layer/tar_generate.go 16.66% 19 Missing and 1 partial ⚠️
cmd/umoci/config.go 21.73% 18 Missing ⚠️
utils.go 14.28% 16 Missing and 2 partials ⚠️
cmd/umoci/raw-add-layer.go 33.33% 10 Missing ⚠️
oci/casext/refname.go 10.00% 9 Missing ⚠️
... and 23 more

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main     #556       +/-   ##
===========================================
+ Coverage   46.49%   72.43%   +25.94%     
===========================================
  Files          60       60               
  Lines        4906     4985       +79     
===========================================
+ Hits         2281     3611     +1330     
+ Misses       2358     1000     -1358     
- Partials      267      374      +107     
Files with missing lines Coverage Δ
api.go 100.00% <100.00%> (ø)
cmd/umoci/new.go 91.78% <100.00%> (+91.78%) ⬆️
pkg/hardening/verified_reader.go 83.51% <100.00%> (ø)
pkg/idtools/idtools.go 100.00% <100.00%> (ø)
cmd/umoci/init.go 83.33% <75.00%> (+83.33%) ⬆️
cmd/umoci/unpack.go 73.97% <66.66%> (+73.97%) ⬆️
oci/casext/json.go 50.00% <0.00%> (ø)
oci/config/convert/default.go 98.55% <0.00%> (ø)
oci/config/generate/save.go 78.57% <0.00%> (ø)
cmd/umoci/utils_ux.go 93.28% <75.00%> (+58.38%) ⬆️
... and 27 more

... and 4 files with indirect coverage changes

@cyphar cyphar force-pushed the go-std-error branch 5 times, most recently from 9317136 to d6122d9 Compare November 28, 2024 10:44
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
I used to dislike using %q because once it starts nesting it gets quite
nasty, but we probably should be properly quoting strings in error
messages in case of weird inputs.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
fmt.Errorf will still return an error if the wrapped errors are nil,
which is a noticable departure from errors.Wrap. Unfortunately, umoci
implicitly depends on this in ways that are non-obvious so a
compatibility shim is the safest option for now.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Quite a few of these callers don't need to use fmtcompat, but for
simplicity they have all been converted and we can slowly remove
fmtcompat.Errorf calls when we verify that they are correct.

Some of the obvious "err = nil" logic has been reworked, but there are
still some suble usages of it that will take some time to find and fix.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
For the super-simple case of string errors that don't contain %w, we can
safely use fmt.Errorf or errors.New.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
For cases like

  if err != nil {
      return fmtcompat.Errorf("...: %w", err)
  }

then we don't need fmtcompat.Errorf because the error has already
explcitly been checked.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
These are more complicated checks than the trivial

  if err != nil {
      return fmt.Errorf("...: %w", err)
  }

but are similar and can be ported for the same reasons as the simple
case, except they needed to be hand-checked for correctness.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
The remaining users of fmtcompat.Errorf are all something resembling

  err := someFunc(...)
  return fmtcompat.Errorf("...: %w", err)

which can easily be rewritten to be more standard Go and doing explicit
error checks. This removes the final users of fmtcompat.Errorf.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
fmtcompat.Errorf was only needed as a transitional tool, now that
everything uses native fmt.Errorf(%w) we can remove it entirely.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
@cyphar cyphar marked this pull request as ready for review November 28, 2024 11:12
@cyphar
Copy link
Member Author

cyphar commented Nov 28, 2024

This patchset adds and removes pkg/fmtcompat. I think that it makes it easier to review, so I think it makes sense to keep it (otherwise you would need to do everything in one big patch or rework things in a somewhat unnatural way, otherwise you'll end up with broken commits if you bisect).

@cyphar cyphar merged commit 8f807a3 into opencontainers:main Nov 29, 2024
18 checks passed
@cyphar cyphar deleted the go-std-error branch November 29, 2024 06:46
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.

Switch to Go 1.13 error types
3 participants