fix(master): drop registry response bodies from poll errors (#9)

This commit is contained in:
2026-10-13 08:26:29 +02:00
parent 92e7646221
commit f169eddc4c
2 changed files with 65 additions and 1 deletions
+32 -1
View File
@@ -4,11 +4,16 @@ package registry
import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/shcizo/package-updater/internal/imageref"
)
@@ -49,7 +54,33 @@ func (c *Client) Digest(ctx context.Context, ref string) (string, error) {
remote.WithAuthFromKeychain(c.keychain),
remote.WithUserAgent(c.userAgent))
if err != nil {
return "", fmt.Errorf("head %s: %w", r, err)
var terr *transport.Error
if errors.As(err, &terr) {
return "", fmt.Errorf("lookup %s: %s", r, describe(terr))
}
return "", fmt.Errorf("lookup %s: %w", r, err)
}
return desc.Digest.String(), nil
}
// errorCodePattern matches the registry error codes of the distribution
// spec ("MANIFEST_UNKNOWN"). Anything else is dropped from error text.
var errorCodePattern = regexp.MustCompile(`^[A-Z_]{1,40}$`)
// describe returns the HTTP status and the structured error codes of a
// registry error, without the messages, details or raw body that ggcr copies
// into its error text. The registry host comes from an image name, so
// whatever it answers must not end up in the UI.
func describe(e *transport.Error) string {
msg := fmt.Sprintf("registry answered %d %s", e.StatusCode, http.StatusText(e.StatusCode))
var codes []string
for _, d := range e.Errors {
if errorCodePattern.MatchString(string(d.Code)) {
codes = append(codes, string(d.Code))
}
}
if len(codes) > 0 {
msg += " (" + strings.Join(codes, ", ") + ")"
}
return msg
}