{"record":{"id":"10447f818b55e951","repo":"router-for-me/CLIProxyAPI","slug":"weight-must-not-exceed-d","errorCode":null,"errorMessage":"weight must not exceed %d","messagePattern":"weight must not exceed (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/credentialweight/weight.go","lineNumber":26,"sourceCode":"\t\"strconv\"\n\t\"strings\"\n)\n\nconst (\n\t// Default is used when a credential does not define a weight.\n\tDefault int64 = 1\n\t// Max bounds scheduler arithmetic while allowing practical proportional routing.\n\tMax int64 = 1_000_000\n)\n\n// Normalize validates and normalizes an explicit weight. Non-positive values are\n// valid and normalize to zero, which excludes the credential from weighted routing.\nfunc Normalize(weight int64) (int64, error) {\n\tif weight <= 0 {\n\t\treturn 0, nil\n\t}\n\tif weight > Max {\n\t\treturn 0, fmt.Errorf(\"weight must not exceed %d\", Max)\n\t}\n\treturn weight, nil\n}\n\n// ParseString parses a scheduler attribute. An empty value uses the default weight.\nfunc ParseString(raw string) (int64, error) {\n\traw = strings.TrimSpace(raw)\n\tif raw == \"\" {\n\t\treturn Default, nil\n\t}\n\tweight, errParse := strconv.ParseInt(raw, 10, 64)\n\tif errParse != nil {\n\t\treturn 0, fmt.Errorf(\"weight must be an integer: %w\", errParse)\n\t}\n\treturn Normalize(weight)\n}\n\n// ParseValue parses a JSON-compatible auth-file metadata value.","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/credentialweight/weight.go#L8-L44","documentation":"Core range check in credentialweight.Normalize: an explicit positive weight above the package Max (1,000,000) is rejected. The cap bounds scheduler arithmetic while still allowing practical proportional routing. This is the error wrapped by the config-level per-key weight errors.","triggerScenarios":"Calling credentialweight.Normalize (directly or via ValidateCredentialWeight / ParseString / ParseValue) with an int64 greater than 1000000. Values <= 0 do not trigger it — they normalize to 0 (excluded from routing).","commonSituations":"Importing weights from an external system with a different scale, computing weights programmatically (e.g. multiplying ratios by 10^7 for precision), or unit tests using huge sentinel values.","solutions":["Clamp or rescale weights to fit in 1..1000000 before passing them in","If weights come from user input or an external feed, normalize with a proportional rescale: w_i = max(1, round(w_i * Max / maxW))","For excluded credentials, pass 0 or a negative value instead of a huge number as a sentinel"],"exampleFix":"// before\nw, err := credentialweight.Normalize(5_000_000) // error\n\n// after\nw, err := credentialweight.Normalize(min(requested, credentialweight.Max))","handlingStrategy":"validation","validationCode":"const maxW = int64(1_000_000)\n\nfunc safeWeight(w int64) int64 {\n    if w <= 0 {\n        return 0 // excluded from routing\n    }\n    if w > maxW {\n        return maxW\n    }\n    return w\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp externally sourced weights to credentialweight.Max before use","Store the cap (1000000) as a constant next to your weight logic so it stays in sync","Unit-test boundary values 1000000 (ok) and 1000001 (reject) in your own wrappers"],"tags":["validation","load-balancing","go","credentialweight"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}