diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 9c883053..b07b9995 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -2195,6 +2195,11 @@ func upstreamMetadataStatus(resp *http.Response) error { body, _ := io.ReadAll(io.LimitReader(resp.Body, upstreamNotFoundBodyLimit)) return &upstreamNotFoundError{body: body} } + // A server error or a 429 is logged by its status alone, without the + // upstream's error page; as in registries' fetcher, no StatusError. + if resp.StatusCode >= http.StatusInternalServerError || resp.StatusCode == http.StatusTooManyRequests { + return fmt.Errorf("upstream returned %d", resp.StatusCode) + } // The status, body and type are kept, so a policy denial can be // forwarded as upstream sent it. body, _ := io.ReadAll(io.LimitReader(resp.Body, upstreamStatusBodyLimit)) diff --git a/internal/handler/metadata_block_test.go b/internal/handler/metadata_block_test.go index f9ac34d0..3e8ce8dd 100644 --- a/internal/handler/metadata_block_test.go +++ b/internal/handler/metadata_block_test.go @@ -3,14 +3,17 @@ package handler import ( "bytes" "context" + "errors" "fmt" "io" "net/http" "net/http/httptest" + "strings" "testing" "time" "github.com/git-pkgs/cooldown" + "github.com/git-pkgs/registries/fetch" ) // A firewall that refuses a metadata request reaches the client with its own @@ -281,3 +284,23 @@ func TestMetadataPolicyBlockServesTheCachedCopy(t *testing.T) { t.Error("the second request wasn't sent upstream") } } + +// A server error or a 429 is logged by its status alone, not with the +// upstream's error page. +func TestUpstreamMetadataStatusKeepsServerErrorsShort(t *testing.T) { + for _, code := range []int{http.StatusTooManyRequests, http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable} { + resp := &http.Response{StatusCode: code, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("an error page"))} + err := upstreamMetadataStatus(resp) + var statusErr *fetch.StatusError + if want := fmt.Sprintf("upstream returned %d", code); err == nil || err.Error() != want || errors.As(err, &statusErr) { + t.Errorf("%d: error %v, want %q", code, err, want) + } + } + for _, code := range []int{http.StatusForbidden, http.StatusConflict, http.StatusGone} { + resp := &http.Response{StatusCode: code, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("denied"))} + var statusErr *fetch.StatusError + if err := upstreamMetadataStatus(resp); !errors.As(err, &statusErr) || statusErr.Code != code || statusErr.Body != "denied" { + t.Errorf("%d: error %v, want a StatusError with the body", code, err) + } + } +}