internal/worker: don't report raw-latest user errors

If the error in computing the raw latest version is from the go.mod
file, don't report it because it's not our bug.

Change-Id: I7c219cc1e8a19b280d0fb7c0f7ef68db296a5dfb
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/296817
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/worker/fetch.go b/internal/worker/fetch.go
index 938d610..e89b0a5 100644
--- a/internal/worker/fetch.go
+++ b/internal/worker/fetch.go
@@ -16,6 +16,7 @@
 	"unicode/utf8"
 
 	"go.opencensus.io/trace"
+	"golang.org/x/mod/modfile"
 	"golang.org/x/mod/semver"
 	"golang.org/x/pkgsite/internal"
 	"golang.org/x/pkgsite/internal/cache"
@@ -72,7 +73,9 @@
 	if err := f.fetchAndUpdateRawLatest(ctx, modulePath); err != nil {
 		// Do not fail the fetch just because we couldn't update the raw latest info.
 		log.Errorf(ctx, "updating raw latest: %v", err)
-		derrors.Report(err)
+		if isInternalError(err) {
+			derrors.Report(err)
+		}
 	}
 
 	ft := f.fetchAndInsertModule(ctx, modulePath, requestedVersion)
@@ -133,6 +136,19 @@
 	return ft.Status, ft.ResolvedVersion, ft.Error
 }
 
+// isInternalError reports whether the error is a problem with the server, rather than
+// the incoming data.
+func isInternalError(err error) bool {
+	// Errors from modfile.ParseLax are user errors.
+	if e := (modfile.ErrorList{}); errors.As(err, &e) {
+		return false
+	}
+	if e := (&modfile.Error{}); errors.As(err, &e) {
+		return false
+	}
+	return true
+}
+
 // fetchAndInsertModule fetches the given module version from the module proxy
 // or (in the case of the standard library) from the Go repo and writes the
 // resulting data to the database.
diff --git a/internal/worker/fetch_test.go b/internal/worker/fetch_test.go
index 52f09e9..a16357e 100644
--- a/internal/worker/fetch_test.go
+++ b/internal/worker/fetch_test.go
@@ -13,6 +13,7 @@
 
 	"github.com/google/go-cmp/cmp"
 	"github.com/google/go-cmp/cmp/cmpopts"
+	"golang.org/x/mod/modfile"
 	"golang.org/x/pkgsite/internal"
 	"golang.org/x/pkgsite/internal/godoc"
 	"golang.org/x/pkgsite/internal/licenses"
@@ -387,3 +388,10 @@
 		t.Errorf("got (%q, %q), want (%q, %q)", got.ModulePath, got.Version, modulePath, wantVersion)
 	}
 }
+
+func TestIsInternalError(t *testing.T) {
+	_, err := modfile.ParseLax("test", []byte("module m\nrequire github.com/googleapis/gax-go v2.0.4"), nil)
+	if isInternalError(err) {
+		t.Errorf("%v (%[1]T): should be user error, got isInternalError=true", err)
+	}
+}