siyuan-note/siyuan · error

invalid bazaar rating region

Error message

invalid bazaar rating region

What it means

fetchBazaarRatingRegion downloads a pre-templated CDN JSON file holding per-package rating distributions for one region bucket. The region parameter indexes bazaarRatingRegionFiles, so any region outside [0, bazaarRatingRegionCount) has no corresponding file and the function rejects it up front with this error. It is a guard against programmer error rather than a network condition.

Solutions

  1. Check the region value passed in and clamp/validate it to 0 <= region < bazaarRatingRegionCount before calling
  2. If you intended to add a region, add its filename to bazaarRatingRegionFiles and bump bazaarRatingRegionCount together
  3. Fix loop bounds so the region index is derived with `<` not `<=`
  4. Log the offending region value at the call site to find where the out-of-range index originates

Example fix

// before
ratings, err := fetchBazaarRatingRegion(ctx, region)
// after
if region < 0 || region >= bazaarRatingRegionCount {
    return fmt.Errorf("region %d out of range", region)
}
ratings, err := fetchBazaarRatingRegion(ctx, region)
Defensive patterns

Strategy: validation

Validate before calling

if region < 0 || region >= bazaarRatingRegionCount {
    return fmt.Errorf("region %d out of range [0,%d)", region, bazaarRatingRegionCount)
}

Try / catch

dist, err := fetchBazaarRatingRegion(ctx, region)
if err != nil && strings.Contains(err.Error(), "invalid bazaar rating region") {
    log.Errorf("bad region index %d, check region config/constants", region)
    return err
}

Prevention

When it happens

Trigger: Calling fetchBazaarRatingRegion with a negative region, or a region >= bazaarRatingRegionCount — e.g. a caller computing the region from user input or a shuffled shard list that got out of sync with the region-count constant.

Common situations: A new region file was added to the CDN but bazaarRatingRegionCount/RegionFiles were not updated, so a caller requests the new index; a loop bound uses len of a differently sized collection; off-by-one when iterating regions (using <= instead of <).

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/c0c10992193f7c8b. Report an issue: GitHub.

Appendix: source

Thrown at kernel/bazaar/rating.go:409

	cache.mu.RLock()
	defer cache.mu.RUnlock()
	ret.loaded = cache.loaded
	fresh = cache.loaded && now.Before(cache.expiresAt)
	if !cache.loaded {
		return
	}
	ret.data = cloneBazaarRatingDistributions(cache.data)
	for packageName, override := range cache.overrides {
		if now.Before(override.expiresAt) {
			ret.data[packageName] = override.distribution
		}
	}
	return
}

func fetchBazaarRatingRegion(ctx context.Context, region int) (ret map[string]bazaarRatingDistribution, err error) {
	if region < 0 || bazaarRatingRegionCount <= region {
		return nil, errors.New("invalid bazaar rating region")
	}

	timeBucket := bazaarRatingNow().Unix() / int64(bazaarRatingCDNBucket/time.Second)
	u := fmt.Sprintf("%s/bazaar/ratings/v1/%s?t=%d", bazaarRatingStatServer, bazaarRatingRegionFiles[region], timeBucket)
	buf := &bytes.Buffer{}
	resp, err := httpclient.NewBrowserRequest().SetRetryCount(0).SetContext(ctx).SetOutput(buf).Get(u)
	if nil != err {
		return nil, err
	}
	if 200 != resp.StatusCode {
		return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}

	ret, err = parseBazaarRatingRegion(buf.Bytes())
	return
}

func parseBazaarRatingRegion(data []byte) (ret map[string]bazaarRatingDistribution, err error) {

View on GitHub (pinned to 9f775e8a12)