siyuan-note/siyuan · error

invalid rating distribution length for package: %s

Error message

invalid rating distribution length for package: %s

What it means

After unmarshalling a package's rating distribution, parseBazaarRatingRegion requires exactly 5 values (counts for star ratings 1-5). A distribution array of any other length aborts parsing with this error naming the package. It guards against schema drift or corruption in the rating payload.

Source

Thrown at kernel/bazaar/rating.go:442

	return
}

func parseBazaarRatingRegion(data []byte) (ret map[string]bazaarRatingDistribution, err error) {
	raw := map[string]json.RawMessage{}
	if err = json.Unmarshal(data, &raw); nil != err {
		return nil, err
	}
	ret = make(map[string]bazaarRatingDistribution, len(raw))
	for packageName, rawDistribution := range raw {
		if !IsValidPackageName(packageName) {
			return nil, fmt.Errorf("invalid package name: %s", packageName)
		}
		var values []int64
		if err = json.Unmarshal(rawDistribution, &values); nil != err {
			return nil, err
		}
		if 5 != len(values) {
			return nil, fmt.Errorf("invalid rating distribution length for package: %s", packageName)
		}
		distribution := bazaarRatingDistribution(values)
		if !validBazaarRatingDistribution(distribution) {
			return nil, fmt.Errorf("invalid rating distribution for package: %s", packageName)
		}
		ret[packageName] = distribution
	}
	return
}

func mergeBazaarRatingRegions(regions [bazaarRatingRegionCount]bazaarRatingRegionResult) map[string]*PackageRating {
	distributions := map[string]bazaarRatingDistribution{}
	for _, region := range regions {
		for packageName, distribution := range region.data {
			merged := distributions[packageName]
			valid := true
			for i, count := range distribution {
				if count > math.MaxInt64-merged[i] {

View on GitHub (pinned to 8641553a1f)

Solutions

  1. Inspect the named package's distribution array in the raw JSON and compare its length to the expected 5
  2. Fix the test/fixture data to contain exactly 5 values
  3. If upstream changed the schema, update the parser and bazaarRatingDistribution type to the new arity
  4. Re-fetch the payload if truncation (network/proxy) is suspected

Example fix

// before
"pkg": [3, 1]
// after
"pkg": [3, 1, 0, 0, 0] // exactly five star buckets (1..5 stars)
Defensive patterns

Strategy: validation

Validate before calling

var raw map[string]json.RawMessage
json.Unmarshal(data, &raw)
for name, dist := range raw {
    var values []int64
    if json.Unmarshal(dist, &values) == nil && len(values) != 5 {
        return fmt.Errorf("package %s has %d rating buckets, need 5", name, len(values))
    }
}

Type guard

func isFiveBucketDistribution(raw json.RawMessage) bool {
    var v []int64
    return json.Unmarshal(raw, &v) == nil && len(v) == 5
}

Try / catch

dist, err := parseBazaarRatingRegion(data)
if err != nil && strings.Contains(err.Error(), "invalid rating distribution length") {
    // re-fetch or skip the payload; log the package name from the error
}

Prevention

When it happens

Trigger: Calling parseBazaarRatingRegion / fetchBazaarRatingRegion on JSON where some package's value array has fewer or more than 5 integers — e.g. [0,0,0,0] or a 6-element array with an extra 'unrated' bucket.

Common situations: Upstream changed the distribution schema (added/removed buckets); a truncated download cut off array elements; a hand-written test fixture used the wrong array length.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/1a83eb1612cd3f1c. Report an issue: GitHub.