Skip to content

Commit b248e48

Browse files
committed
internal/fetch: report DocTooLarge error in goPackage.err
loadPackage has two return values: a *goPackage and an error. Before this CL, if a package's documentation was too large, loadPackage would return the package and a non-nil error. With this CL, that error is stored in the goPackage struct in a new field, and loadPackage returns a nil error. Aside from being more idiomatic, this change will enable future changes needed to support multiple GOOS/GOARCH combinations. For golang/go#37232 Change-Id: If73adde76239046eb22409659d6795c09bbb1fd4 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/279457 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent f5ca8b0 commit b248e48

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

internal/fetch/load.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var goEnvs = []struct{ GOOS, GOARCH string }{
5959
// loadPackage returns nil, nil.
6060
//
6161
// If the package is fine except that its documentation is too large, loadPackage
62-
// returns both a package and a non-nil error with godoc.ErrTooLarge in its chain.
62+
// returns a package whose err field is a non-nil error with godoc.ErrTooLarge in its chain.
6363
func loadPackage(ctx context.Context, zipGoFiles []*zip.File, innerPath string, sourceInfo *source.Info, modInfo *godoc.ModuleInfo) (_ *goPackage, err error) {
6464
defer derrors.Wrap(&err, "loadPackage(ctx, zipGoFiles, %q, sourceInfo, modInfo)", innerPath)
6565
ctx, span := trace.StartSpan(ctx, "fetch.loadPackage")
@@ -70,7 +70,8 @@ func loadPackage(ctx context.Context, zipGoFiles []*zip.File, innerPath string,
7070
return nil, err
7171
}
7272
if pkg != nil {
73-
return pkg, err
73+
pkg.err = err
74+
return pkg, nil
7475
}
7576
}
7677
return nil, nil
@@ -88,9 +89,9 @@ var httpPost = http.Post
8889
// zipGoFiles must contain only .go files that have been verified
8990
// to be of reasonable size.
9091
//
91-
// The returned Package.Licenses field is not populated.
92+
// The returned goPackage.licenses field is not populated.
9293
//
93-
// It returns a nil Package if the directory doesn't contain a Go package
94+
// It returns a nil goPackage if the directory doesn't contain a Go package
9495
// or all .go files have been excluded by constraints.
9596
// A *BadPackageError error is returned if the directory
9697
// contains .go files but do not make up a valid package.
@@ -115,8 +116,7 @@ func loadPackageWithBuildContext(ctx context.Context, goos, goarch string, zipGo
115116
docPkg.AddFile(pf, removeNodes)
116117
}
117118

118-
// Encode before rendering: both operations mess with the AST, but Encode restores
119-
// it enough to make Render work.
119+
// Encode first, because Render messes with the AST.
120120
src, err := docPkg.Encode(ctx)
121121
if err != nil {
122122
return nil, err

internal/fetch/package.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type goPackage struct {
4343
// series.
4444
v1path string
4545
source []byte // the source files of the package, for generating doc at serving time
46+
err error // non-fatal error when loading the package (e.g. documentation is too large)
4647
}
4748

4849
// extractPackagesFromZip returns a slice of packages from the module zip r.
@@ -197,13 +198,9 @@ func extractPackagesFromZip(ctx context.Context, modulePath, resolvedVersion str
197198
incompleteDirs[innerPath] = true
198199
status = derrors.PackageInvalidContents
199200
errMsg = err.Error()
200-
} else if errors.Is(err, godoc.ErrTooLarge) {
201-
status = derrors.PackageDocumentationHTMLTooLarge
202-
errMsg = err.Error()
203201
} else if err != nil {
204202
return nil, nil, fmt.Errorf("unexpected error loading package: %v", err)
205203
}
206-
207204
var pkgPath string
208205
if pkg == nil {
209206
// No package.
@@ -214,6 +211,10 @@ func extractPackagesFromZip(ctx context.Context, modulePath, resolvedVersion str
214211
}
215212
pkgPath = path.Join(modulePath, innerPath)
216213
} else {
214+
if errors.Is(pkg.err, godoc.ErrTooLarge) {
215+
status = derrors.PackageDocumentationHTMLTooLarge
216+
errMsg = pkg.err.Error()
217+
}
217218
if d != nil { // should only be nil for tests
218219
isRedist, lics := d.PackageInfo(innerPath)
219220
pkg.isRedistributable = isRedist

0 commit comments

Comments
 (0)