{"record":{"id":"12fe11a4c0591759","repo":"gofiber/fiber","slug":"fasthttp-lbclient-must-not-be-nil","errorCode":null,"errorMessage":"fasthttp.LBClient must not be nil","messagePattern":"fasthttp\\.LBClient must not be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"client/client.go","lineNumber":952,"sourceCode":"func NewWithClient(c *fasthttp.Client) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.Client must not be nil\")\n\t}\n\treturn newClient(newStandardClientTransport(c))\n}\n\n// NewWithHostClient creates and returns a new Client object from an existing fasthttp.HostClient.\nfunc NewWithHostClient(c *fasthttp.HostClient) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.HostClient must not be nil\")\n\t}\n\treturn newClient(newHostClientTransport(c))\n}\n\n// NewWithLBClient creates and returns a new Client object from an existing fasthttp.LBClient.\nfunc NewWithLBClient(c *fasthttp.LBClient) *Client {\n\tif c == nil {\n\t\tpanic(\"fasthttp.LBClient must not be nil\")\n\t}\n\treturn newClient(newLBClientTransport(c))\n}\n\nfunc newClient(transport httpClientTransport) *Client {\n\treturn &Client{\n\t\ttransport: transport,\n\t\theader: &Header{\n\t\t\tRequestHeader: &fasthttp.RequestHeader{},\n\t\t},\n\t\tparams: &QueryParam{\n\t\t\tArgs: fasthttp.AcquireArgs(),\n\t\t},\n\t\tcookies: &Cookie{},\n\t\tpath:    &PathParam{},\n\n\t\tuserRequestHooks:     []RequestHook{},\n\t\tbuiltinRequestHooks:  []RequestHook{parserRequestURL, parserRequestHeader, parserRequestBody},","sourceCodeStart":934,"sourceCodeEnd":970,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/client/client.go#L934-L970","documentation":"NewWithLBClient wraps a *fasthttp.LBClient (load-balancing client across multiple hosts). Passing nil panics immediately because the LB client is the transport core. Use this when you need client-side balancing across several backends.","triggerScenarios":"Calling fiber.NewWithLBClient(nil), or passing an LBClient built from an empty backend list helper that returned nil.","commonSituations":"Configuring backends from an env var that was empty; refactor that replaced a multi-host setup with a helper returning nil on zero backends; testing stub that forgot to populate the client.","solutions":["Build the LBClient with at least one backend: &fasthttp.LBClient{Clients: []*fasthttp.HostClient{...}}.","Guard against empty backend lists and fall back to NewWithHostClient or New().","Validate backend config before constructing the client."],"exampleFix":"// before\nlb := buildLBClient(cfg.Backends) // returns nil when Backends empty\nclient := fiber.NewWithLBClient(lb)\n\n// after\nif len(cfg.Backends) == 0 { return errors.New(\"no backends\") }\nclient := fiber.NewWithLBClient(buildLBClient(cfg.Backends))","handlingStrategy":"validation","validationCode":"func newWithLBClientSafe(c *fasthttp.LBClient) (*fiber.Client, error) {\n    if c == nil { return nil, errors.New(\"LBClient is nil\") }\n    return fiber.NewWithLBClient(c), nil\n}","typeGuard":null,"tryCatchPattern":"func safeNewWithLBClient(c *fasthttp.LBClient) (cl *fiber.Client, err error) {\n    defer func() {\n        if r := recover(); r != nil { err = fmt.Errorf(\"%v\", r) }\n    }()\n    cl = fiber.NewWithLBClient(c)\n    return cl, nil\n}","preventionTips":["Validate the backends list is non-empty before building the LBClient.","Provide a fallback single-host client when only one backend is configured.","Wrap the constructor with an error-returning helper."],"tags":["client","nil-guard","constructor","load-balancing"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}