{"record":{"id":"0bbf42496e4a84b3","repo":"thanos-io/thanos","slug":"negative-s-is-not-accepted-try-a-positive-inte","errorCode":null,"errorMessage":"negative '%s' is not accepted. Try a positive integer","messagePattern":"negative '(.+?)' is not accepted\\. Try a positive integer","errorType":"validation","errorClass":"api.ApiError","httpStatus":400,"severity":"error","filePath":"pkg/api/query/v1.go","lineNumber":383,"sourceCode":"}\n\nfunc (qapi *QueryAPI) parseDownsamplingParamMillis(r *http.Request, defaultVal time.Duration) (maxResolutionMillis int64, _ *api.ApiError) {\n\tmaxSourceResolution := 0 * time.Second\n\n\tval := r.FormValue(MaxSourceResolutionParam)\n\tif qapi.enableAutodownsampling || (val == \"auto\") {\n\t\tmaxSourceResolution = defaultVal\n\t}\n\tif val != \"\" && val != \"auto\" {\n\t\tvar err error\n\t\tmaxSourceResolution, err = parseDuration(val)\n\t\tif err != nil {\n\t\t\treturn 0, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Wrapf(err, \"'%s' parameter\", MaxSourceResolutionParam)}\n\t\t}\n\t}\n\n\tif maxSourceResolution < 0 {\n\t\treturn 0, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Errorf(\"negative '%s' is not accepted. Try a positive integer\", MaxSourceResolutionParam)}\n\t}\n\n\treturn int64(maxSourceResolution / time.Millisecond), nil\n}\n\nfunc (qapi *QueryAPI) parsePartialResponseParam(r *http.Request, defaultEnablePartialResponse bool) (enablePartialResponse bool, _ *api.ApiError) {\n\t// Overwrite the cli flag when provided as a query parameter.\n\tif val := r.FormValue(PartialResponseParam); val != \"\" {\n\t\tvar err error\n\t\tdefaultEnablePartialResponse, err = strconv.ParseBool(val)\n\t\tif err != nil {\n\t\t\treturn false, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Wrapf(err, \"'%s' parameter\", PartialResponseParam)}\n\t\t}\n\t}\n\treturn defaultEnablePartialResponse, nil\n}\n\nfunc (qapi *QueryAPI) parseStep(r *http.Request, defaultRangeQueryStep time.Duration, rangeSeconds int64) (time.Duration, *api.ApiError) {","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/pkg/api/query/v1.go#L365-L401","documentation":"After successfully parsing max_source_resolution, parseDownsamplingParamMillis rejects negative durations with a dedicated ErrorBadData message \"negative 'max_source_resolution' is not accepted. Try a positive integer\". A negative resolution is meaningless for downsampling, so it is explicitly refused.","triggerScenarios":"Passing max_source_resolution=-5m, -1h, or any negative duration on query/query_range or explain endpoints, typically from templated dashboards or arithmetic in scripts producing negative values.","commonSituations":"Grafana variables computed from interval math going negative; shell scripts doing $(($a - $b)) with reversed operands; copying a step value that was negated.","solutions":["Clamp the value to a positive duration (or zero/auto) in the client before sending","Fix the sign of the computed expression producing the negative value","Add client-side validation: reject values starting with '-' or parse and check < 0","Use 'auto' instead of a computed value when unsure"],"exampleFix":"// before\nres=\"-$interval\"  # -5m\n// after\nres=\"$interval\"   # 5m; or Math.max(0, parsed) in tooling","handlingStrategy":"validation","validationCode":"function validatePositiveDuration(v) {\n  if (typeof v === 'string' && (v === 'auto' || !v.startsWith('-'))) return v;\n  const ms = parsePromDurationToMs(v);\n  if (ms < 0) throw new Error('max_source_resolution must be positive');\n  return v;\n}","typeGuard":"null","tryCatchPattern":"try {\n  const res = await fetch(url);\n  const j = await res.json();\n  if (j.status === 'error' && /negative.*max_source_resolution/.test(j.error)) throw new Error(j.error);\n} catch (e) {\n  if (/negative.*max_source_resolution/.test(e.message)) { /* clamp to 0 or 'auto' and retry */ }\n  throw e;\n}","preventionTips":["Clamp computed intervals to >= 0 before use","Check sign when doing interval arithmetic in scripts/shell","Add assertions in dashboard variable expressions","Treat 0/auto as the safe fallback"],"tags":["http-api","query-parameter","validation","thanos"],"backgroundTag":"value-out-of-range","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}