router-for-me/CLIProxyAPI · warning

weight must be an integer

Error message

weight must be an integer

What it means

parseCredentialWeightPatch could not JSON-decode the weight value into an int — the raw bytes are valid JSON but not an integer literal: a string ("3"), a float (2.5), a bool, an array/object, or trailing content after the number. Weights must be whole numbers and are further constrained by config.ValidateCredentialWeight.

Source

Thrown at internal/api/handlers/management/config_lists.go:23

	"encoding/json"
	"fmt"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
)

func parseCredentialWeightPatch(raw json.RawMessage) (*int, error) {
	if len(raw) == 0 {
		return nil, fmt.Errorf("weight is missing")
	}
	if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
		return nil, nil
	}
	var weight int
	decoder := json.NewDecoder(bytes.NewReader(raw))
	if errDecode := decoder.Decode(&weight); errDecode != nil {
		return nil, fmt.Errorf("weight must be an integer")
	}
	if errValidate := config.ValidateCredentialWeight(&weight); errValidate != nil {
		return nil, errValidate
	}
	return &weight, nil
}

func rejectInvalidCredentialWeight(c *gin.Context, field string, weight *int) bool {
	if errValidate := config.ValidateCredentialWeight(weight); errValidate != nil {
		c.JSON(400, gin.H{"error": fmt.Sprintf("%s: %v", field, errValidate)})
		return true
	}
	return false
}

// Generic helpers for list[string]
func (h *Handler) putStringList(c *gin.Context, set func([]string), after func()) {
	data, err := c.GetRawData()

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Send a plain JSON integer for weight (no quotes, no decimals)
  2. Validate client-side before the request that weight is an integer within the configured bounds
  3. If a fractional or quoted value is legitimate in your UX, convert to int before sending
  4. Check config.ValidateCredentialWeight's bounds so the follow-up validation does not reject the value

Example fix

// before
{"weight": "3"}
// after
{"weight": 3}
Defensive patterns

Strategy: type-guard

Validate before calling

var w float64
if err := json.Unmarshal(raw, &w); err != nil || w != math.Trunc(w) {
    return errors.New("weight must be an integer")
}

Type guard

func isJSONInt(raw json.RawMessage) bool {
    var w int
    dec := json.NewDecoder(bytes.NewReader(raw))
    return dec.Decode(&w) == nil && dec.More() == false
}

Try / catch

if err := decoder.Decode(&weight); err != nil {
    c.JSON(400, gin.H{"error": "weight must be a JSON integer, e.g. 3"})
}

Prevention

When it happens

Trigger: PATCHing a credential's weight with 2.5, "3", true, or {"value":3}; clients sending weights as strings because their config layer typed them as text.

Common situations: Frontend forms binding weight to a text input; loosely typed (any) payloads in TypeScript clients; JSON generators quoting numbers; locale formatting inserting decimal separators.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/72a11fe3ed9aea21. Report an issue: GitHub.