{"record":{"id":"4a950b566c430771","repo":"siyuan-note/siyuan","slug":"rating-must-be-an-integer-from-0-to-5","errorCode":null,"errorMessage":"rating must be an integer from 0 to 5","messagePattern":"rating must be an integer from 0 to 5","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/bazaar_rating.go","lineNumber":154,"sourceCode":"\t\t\"token\":       token,\n\t\t\"packageName\": packageName,\n\t}, &data)\n\tif nil != err {\n\t\treturn nil, false, 0, err\n\t}\n\tif 0 > data.Rating || 5 < data.Rating {\n\t\treturn nil, false, 0, errors.New(\"invalid user rating returned by cloud server\")\n\t}\n\n\trating, ratingAvailable = bazaar.GetBazaarPackageRating(ctx, packageName)\n\treturn rating, ratingAvailable, data.Rating, nil\n}\n\n// SetBazaarPackageRating 设置或取消已安装官方包的当前用户评分。\nfunc SetBazaarPackageRating(ctx context.Context, pkgType, packageName string, userRating int) (rating *bazaar.PackageRating,\n\tratingAvailable bool, retUserRating int, err error) {\n\tif 0 > userRating || 5 < userRating {\n\t\treturn nil, false, 0, errors.New(\"rating must be an integer from 0 to 5\")\n\t}\n\tbazaarRatingSetMu.Lock()\n\tdefer bazaarRatingSetMu.Unlock()\n\n\ttoken, err := bazaarRatingValidatePackage(ctx, pkgType, packageName)\n\tif nil != err {\n\t\treturn nil, false, 0, err\n\t}\n\n\tregion := util.CurrentCloudRegion\n\tdata := bazaarPackageSetRatingData{}\n\terr = requestBazaarPackageRating(ctx, \"/apis/siyuan/bazaar/setBazaarPackageRating\", map[string]any{\n\t\t\"token\":       token,\n\t\t\"packageName\": packageName,\n\t\t\"rating\":      userRating,\n\t}, &data)\n\tif nil != err {\n\t\treturn nil, false, 0, err","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/bazaar_rating.go#L136-L172","documentation":"Range validation at the top of SetBazaarPackageRating: fires when userRating is outside 0..5 (or the cancellation path mishandles it), before any cloud request is made. Ratings are integers 0–5 where cancellation uses a sentinel; an out-of-range value is the faulting input, unlike the separate error for invalid ratings returned by the cloud server.","triggerScenarios":"Calling SetBazaarPackageRating with userRating < 0 or > 5, e.g. -1 for 'unset' or a 1..5 star value shifted off by one.","commonSituations":"UI code using 1-5 stars and passing 5 as 'no rating' instead of 0; off-by-one in star widget mapping; passing an unbounded parsed integer from user input.","solutions":["Pass 0 to cancel a rating, 1-5 to set one; clamp/validate input before calling","Map the UI star value with 0 as the 'none' sentinel","Check bounds (0 <= r <= 5) at the API boundary"],"exampleFix":"// before\nmodel.SetBazaarPackageRating(ctx, \"plugins\", name, -1) // cancel attempt\n// after\nmodel.SetBazaarPackageRating(ctx, \"plugins\", name, 0) // 0 cancels the rating","handlingStrategy":"validation","validationCode":"if userRating < 0 || userRating > 5 {\n    return errors.New(\"rating must be an integer from 0 to 5\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use 0 as the single 'cancel rating' sentinel everywhere","Clamp star-widget values to 1..5 and map 'none' to 0","Validate user input at the UI boundary before invoking the kernel"],"tags":["validation","argument","range"],"backgroundTag":"value-out-of-range","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}