{"record":{"id":"5fa80d6f63644ee5","repo":"ipfs/kubo","slug":"number-of-providers-must-be-greater-than-0","errorCode":null,"errorMessage":"number of providers must be greater than 0","messagePattern":"number of providers must be greater than 0","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/coreapi/routing.go","lineNumber":113,"sourceCode":"\tsettings, err := caopts.RoutingFindProvidersOptions(opts...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tspan.SetAttributes(attribute.Int(\"numproviders\", settings.NumProviders))\n\n\terr = api.checkOnline(false)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\trp, _, err := api.core().ResolvePath(ctx, p)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tnumProviders := settings.NumProviders\n\tif numProviders < 1 {\n\t\treturn nil, errors.New(\"number of providers must be greater than 0\")\n\t}\n\n\tpchan := api.routing.FindProvidersAsync(ctx, rp.RootCid(), numProviders)\n\treturn pchan, nil\n}\n\nfunc (api *RoutingAPI) Provide(ctx context.Context, path path.Path, opts ...caopts.RoutingProvideOption) error {\n\tctx, span := tracing.Span(ctx, \"CoreAPI.DhtAPI\", \"Provide\", trace.WithAttributes(attribute.String(\"path\", path.String())))\n\tdefer span.End()\n\n\tsettings, err := caopts.RoutingProvideOptions(opts...)\n\tif err != nil {\n\t\treturn err\n\t}\n\tspan.SetAttributes(attribute.Bool(\"recursive\", settings.Recursive))\n\n\terr = api.checkOnline(false)\n\tif err != nil {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/coreapi/routing.go#L95-L131","documentation":"FindProviders validates settings.NumProviders before querying the routing system for provider records. A count below 1 would yield zero results from FindProvidersAsync, so the API rejects it explicitly as a programming/input error.","triggerScenarios":"Calling Routing().FindProviders with NumProviders set to 0 or a negative value — typically because a limit/MaxResults variable was zero-valued, defaulted incorrectly, or parsed from user input without validation.","commonSituations":"Code that passes an unset integer field as the provider count, CLI wrappers mapping a --count flag defaulting to 0, and generic search helpers that reuse the same settings struct with the field unpopulated.","solutions":["Set NumProviders to a positive integer (commonly 10-20) before calling FindProviders.","If you want a sane default, use the options package default rather than a zero-valued struct.","Validate any user-supplied count and clamp it to >= 1 before calling the API.","Provide options.ProvideEditors/defaults from options.Name() helpers so fields are initialized."],"exampleFix":"// before\nopts := options.Routing.FindProviders() // NumProviders left 0\nprovs, err := api.Routing().FindProviders(ctx, p, opts)\n// after\nopts.NumProviders = 20\nprovs, err := api.Routing().FindProviders(ctx, p, opts)","handlingStrategy":"validation","validationCode":"if settings.NumProviders < 1 {\n\tsettings.NumProviders = 20 // or return a clear client-side error\n}\nprovs, err := api.Routing().FindProviders(ctx, p, settings)","typeGuard":null,"tryCatchPattern":"provs, err := api.Routing().FindProviders(ctx, p, settings)\nif err != nil && strings.Contains(err.Error(), \"number of providers\") {\n\tsettings.NumProviders = 20\n\tprovs, err = api.Routing().FindProviders(ctx, p, settings)\n}","preventionTips":["Initialize option structs via the options package constructors, not zero values","Clamp user-supplied counts to a sane minimum before calling","Document that NumProviders is required in wrapper APIs you expose"],"tags":["ipfs","routing","providers","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}