Skip to content

Commit

Permalink
internal/fetch: make readme too large a ModuleTooLarge error
Browse files Browse the repository at this point in the history
A too large README was returning an error with no code, resulting in
a 500 from the worker which would then retry the module. This updates
the error to be a ModuleTooLarge error, preventing it from being
retried.

Change-Id: Ib93d7d34e0104c5fb630286854f2d165b4379042
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/529375
kokoro-CI: kokoro <[email protected]>
Run-TryBot: Suzy Mueller <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
suzmue committed Sep 19, 2023
1 parent 545cf47 commit 14233af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/fetch/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func extractReadmes(modulePath, resolvedVersion string, contentDir fs.FS) (_ []*
return err
}
if info.Size() > MaxFileSize {
return fmt.Errorf("file size %d exceeds max limit %d", info.Size(), MaxFileSize)
return fmt.Errorf("file size %d exceeds max limit %d: %w", info.Size(), MaxFileSize, derrors.ModuleTooLarge)
}
c, err := readFSFile(contentDir, pathname, MaxFileSize)
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions internal/fetch/readme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package fetch

import (
"context"
"errors"
"io/fs"
"sort"
"testing"

"github.com/google/go-cmp/cmp"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/proxy/proxytest"
"golang.org/x/pkgsite/internal/stdlib"
"golang.org/x/pkgsite/internal/testenv"
Expand Down Expand Up @@ -135,6 +137,41 @@ func TestExtractReadmes(t *testing.T) {
}
}

func TestExtractReadmesError(t *testing.T) {
ctx := context.Background()

var (
modulePath = "github.com/my/module"
version = "v1.0.0"
files = map[string]string{
"foo/README.md": string(make([]byte, MaxFileSize+100)),
}
)
var (
contentDir fs.FS
err error
)
proxyClient, teardownProxy := proxytest.SetupTestClient(t, []*proxytest.Module{
{ModulePath: modulePath, Files: files}})
defer teardownProxy()
reader, err := proxyClient.Zip(ctx, modulePath, "v1.0.0")
if err != nil {
t.Fatal(err)
}
contentDir, err = fs.Sub(reader, modulePath+"@v1.0.0")
if err != nil {
t.Fatal(err)
}
got, err := extractReadmes(modulePath, version, contentDir)
if err == nil {
t.Fatalf("want error, got %v", cmp.Diff([]*internal.Readme{}, got))
}

if !errors.Is(err, derrors.ModuleTooLarge) {
t.Errorf("want ModuleTooLarge, got %v", err)
}
}

func TestIsReadme(t *testing.T) {
for _, test := range []struct {
name, file string
Expand Down

0 comments on commit 14233af

Please sign in to comment.