explicitly handle errors when wrapping them

The errors.Wrap and errors.Wrapf functions gracefully handle nil-errors.
This allows them to be used unconditionally regardless if an error
was produced.

While this can be convenient, it can also be err-prone, as replacing
these with stdlib errors means they unconditionally produce an error.

This patch replaces code uses of errors.Wrap to be gated by a check
for nil-errors to future-proof our code.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2025-02-20 18:10:36 +01:00
parent f9ced58158
commit da4b6275ba
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 22 additions and 8 deletions

View File

@ -76,9 +76,9 @@ func (c *Context) preFormat() {
func (c *Context) parseFormat() (*template.Template, error) { func (c *Context) parseFormat() (*template.Template, error) {
tmpl, err := templates.Parse(c.finalFormat) tmpl, err := templates.Parse(c.finalFormat)
if err != nil { if err != nil {
return tmpl, errors.Wrap(err, "template parsing error") return nil, errors.Wrap(err, "template parsing error")
} }
return tmpl, err return tmpl, nil
} }
func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) { func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {

View File

@ -210,8 +210,10 @@ func newVersionTemplate(templateFormat string) (*template.Template, error) {
} }
tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}) tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder})
tmpl, err := tmpl.Parse(templateFormat) tmpl, err := tmpl.Parse(templateFormat)
if err != nil {
return tmpl, errors.Wrap(err, "template parsing error") return nil, errors.Wrap(err, "template parsing error")
}
return tmpl, nil
} }
func getDetailsOrder(v types.ComponentVersion) []string { func getDetailsOrder(v types.ComponentVersion) []string {

View File

@ -67,7 +67,10 @@ func recursiveInterpolate(value any, path Path, opts Options) (any, error) {
return newValue, nil return newValue, nil
} }
casted, err := caster(newValue) casted, err := caster(newValue)
if err != nil {
return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type")) return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type"))
}
return casted, nil
case map[string]any: case map[string]any:
out := map[string]any{} out := map[string]any{}

View File

@ -121,8 +121,11 @@ func (c *client) PutManifest(ctx context.Context, ref reference.Named, manifest
} }
dgst, err := manifestService.Put(ctx, manifest, opts...) dgst, err := manifestService.Put(ctx, manifest, opts...)
if err != nil {
return dgst, errors.Wrapf(err, "failed to put manifest %s", ref) return dgst, errors.Wrapf(err, "failed to put manifest %s", ref)
} }
return dgst, nil
}
func (c *client) getRepositoryForReference(ctx context.Context, ref reference.Named, repoEndpoint repositoryEndpoint) (distribution.Repository, error) { func (c *client) getRepositoryForReference(ctx context.Context, ref reference.Named, repoEndpoint repositoryEndpoint) (distribution.Repository, error) {
repoName, err := reference.WithName(repoEndpoint.Name()) repoName, err := reference.WithName(repoEndpoint.Name())
@ -157,7 +160,10 @@ func (c *client) getHTTPTransportForRepoEndpoint(ctx context.Context, repoEndpoi
c.userAgent, c.userAgent,
repoEndpoint.actions, repoEndpoint.actions,
) )
return httpTransport, errors.Wrap(err, "failed to configure transport") if err != nil {
return nil, errors.Wrap(err, "failed to configure transport")
}
return httpTransport, nil
} }
// GetManifest returns an ImageManifest for the reference // GetManifest returns an ImageManifest for the reference

View File

@ -20,7 +20,10 @@ func parseCount(s string) (int, error) {
return -1, nil return -1, nil
} }
i, err := strconv.Atoi(s) i, err := strconv.Atoi(s)
return i, errors.Wrap(err, "count must be an integer") if err != nil {
return 0, errors.Wrap(err, "count must be an integer")
}
return i, nil
} }
// Set a new mount value // Set a new mount value