siyuan-note/siyuan · error

rating must be an integer from 0 to 5

Error message

rating must be an integer from 0 to 5

What it means

Error returned by model.SetBazaarPackageRating (kernel/model/bazaar_rating.go:138) when the caller-supplied userRating is outside 0..5. This is pure local input validation executed before taking the bazaarRatingSetMu lock and before any network call: 0 means 'clear my rating', 1..5 are star values, and anything else (negative, >5, or a value coerced from non-integer input) is rejected immediately.

Source

Thrown at kernel/model/bazaar_rating.go:138

		"token":       token,
		"packageName": packageName,
	}, &data)
	if nil != err {
		return nil, false, 0, err
	}
	if 0 > data.Rating || 5 < data.Rating {
		return nil, false, 0, errors.New("invalid user rating returned by cloud server")
	}

	rating, ratingAvailable = bazaar.GetBazaarPackageRating(ctx, packageName)
	return rating, ratingAvailable, data.Rating, nil
}

// SetBazaarPackageRating 设置或取消已安装官方包的当前用户评分。
func SetBazaarPackageRating(ctx context.Context, pkgType, packageName string, userRating int) (rating *bazaar.PackageRating,
	ratingAvailable bool, retUserRating int, err error) {
	if 0 > userRating || 5 < userRating {
		return nil, false, 0, errors.New("rating must be an integer from 0 to 5")
	}
	bazaarRatingSetMu.Lock()
	defer bazaarRatingSetMu.Unlock()

	token, err := bazaarRatingValidatePackage(ctx, pkgType, packageName)
	if nil != err {
		return nil, false, 0, err
	}

	region := util.CurrentCloudRegion
	data := bazaarPackageSetRatingData{}
	err = requestBazaarPackageRating(ctx, "/apis/siyuan/bazaar/setBazaarPackageRating", map[string]any{
		"token":       token,
		"packageName": packageName,
		"rating":      userRating,
	}, &data)
	if nil != err {
		return nil, false, 0, err

View on GitHub (pinned to afa823b6b4)

Solutions

  1. Clamp/validate the value to 0..5 before the call (0 clears the rating)
  2. Map your UI's scale to 1..5 stars explicitly and only send a request on explicit user action
  3. Reject form submissions server-side in your client code with the same range check

Example fix

// before
err := setRating(ctx, pkgType, name, rawScore) // rawScore is 0..10 from the UI

// after
stars := (rawScore + 5) / 10 // map 0..10 to 0..5, or better: derive directly from star count
if stars < 0 || stars > 5 {
    return fmt.Errorf("rating must be 0..5, got %d", stars)
}
err := setRating(ctx, pkgType, name, stars)
Defensive patterns

Strategy: validation

Validate before calling

if userRating < 0 || userRating > 5 {
    return fmt.Errorf("rating must be 0..5, got %d", userRating)
}

Type guard

func isValidRating(r int) bool { return r >= 0 && r <= 5 }

Prevention

When it happens

Trigger: Calling /api/bazaar/setBazaarPackageRating with rating=6 from a UI star widget bug, rating=-1 to mean 'remove', or forwarding an unvalidated integer from a request body.

Common situations: Frontends mapping a 0-10 or 0-100 scale onto stars without dividing; 'clear rating' implemented as -1 instead of 0; default zero-value ints accidentally sent as a real 1-star rating when the user made no choice.

Related errors


AI-assisted analysis of siyuan-note/siyuan@afa823b6b4 (2026-08-18). Data as JSON: /api/errors/4a950b566c430771. Report an issue: GitHub.