{"record":{"id":"5ec1042e31a80d70","repo":"gofiber/fiber","slug":"fasthttp-client-must-not-be-nil","errorCode":null,"errorMessage":"fasthttp.Client must not be nil","messagePattern":"fasthttp\\.Client must not be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"client/client.go","lineNumber":936,"sourceCode":"\tdefaultUserAgent = \"fiber\"\n)\n\nfunc init() {\n\tdefaultClient.Store(New())\n}\n\n// New creates and returns a new Client object.\nfunc New() *Client {\n\t// Follow-up performance optimizations:\n\t// Try to use a pool to reduce the memory allocation cost for the Fiber client and the fasthttp client.\n\t// If possible, also consider pooling other structs (e.g., request headers, cookies, query parameters, path parameters).\n\treturn NewWithClient(&fasthttp.Client{})\n}\n\n// NewWithClient creates and returns a new Client object from an existing fasthttp.Client.\nfunc 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))","sourceCodeStart":918,"sourceCodeEnd":954,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/client/client.go#L918-L954","documentation":"NewWithClient wraps a *fasthttp.Client into a Fiber client. Passing nil is unrecoverable because every subsequent request would dereference nil, so the constructor panics eagerly. Use New() if you just want a default fasthttp.Client.","triggerScenarios":"Calling fiber.NewWithClient(nil) directly, or passing a variable that was never initialized (e.g. var c *fasthttp.Client).","commonSituations":"Conditional initialization where the client is only set in one branch; refactor extracting client creation that returns nil on misconfiguration; copy-paste from NewWithHostClient without updating the argument.","solutions":["Pass a non-nil *fasthttp.Client, or use fiber.New() to get a default one.","Initialize the variable at declaration: c := &fasthttp.Client{...} instead of var c *fasthttp.Client.","If the client is optional, branch on nil and use fiber.New() as fallback."],"exampleFix":"// before\nvar c *fasthttp.Client\nclient := fiber.NewWithClient(c)\n\n// after\nclient := fiber.NewWithClient(&fasthttp.Client{ReadTimeout: 5*time.Second})","handlingStrategy":"validation","validationCode":"func newClientSafe(c *fasthttp.Client) (*fiber.Client, error) {\n    if c == nil { return nil, errors.New(\"fasthttp.Client is nil\") }\n    return fiber.NewWithClient(c), nil\n}","typeGuard":null,"tryCatchPattern":"func safeNewWithClient(c *fasthttp.Client) (cl *fiber.Client, err error) {\n    defer func() {\n        if r := recover(); r != nil { err = fmt.Errorf(\"%v\", r) }\n    }()\n    cl = fiber.NewWithClient(c)\n    return cl, nil\n}","preventionTips":["Always initialize the *fasthttp.Client at declaration.","Prefer fiber.New() unless you need a custom fasthttp.Client.","Wrap constructors with a (value, error) helper in your codebase."],"tags":["client","nil-guard","constructor"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}