{"id":"c112fb4561b3751e","repo":"gofiber/fiber","slug":"fasthttp-hostclient-must-not-be-nil","errorCode":null,"errorMessage":"fasthttp.HostClient must not be nil","messagePattern":"fasthttp\\.HostClient must not be nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/client.go","lineNumber":944,"sourceCode":"func 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))\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},","sourceCodeStart":926,"sourceCodeEnd":962,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/client.go#L926-L962","documentation":"Panics from client/client.go:944 inside the fiber client constructor NewWithHostClient when the supplied *fasthttp.HostClient is nil. The fiber HTTP client wraps a fasthttp transport; a nil transport has no connection pool, dialer, or timeout state, so every subsequent request would nil-deref. The library treats this as a programmer error and fails fast at construction rather than corrupting state later.","triggerScenarios":"Calling client.NewWithHostClient(nil), or passing a pointer variable that was declared but never initialized (var hc *fasthttp.HostClient; client.NewWithHostClient(hc)). The panic fires immediately, before any request is sent.","commonSituations":"Conditionally building the HostClient (e.g. only when a feature flag is on) and passing the zero-value pointer when the flag is off; refactoring that moved HostClient creation into a helper that can return nil on a config parse error; tests that stub the transport with nil.","solutions":["Guarantee the *fasthttp.HostClient is allocated before calling NewWithHostClient; construct it inline: client.NewWithHostClient(&fasthttp.HostClient{Addr: \"example.com:80\"}).","If the HostClient comes from a factory that can fail, check for nil first and fall back to client.New() or return the error up the stack.","Add a nil check in the factory and log/return a configuration error instead of propagating a nil pointer."],"exampleFix":"// before\nvar hc *fasthttp.HostClient\napp := client.NewWithHostClient(hc)\n\n// after\napp := client.NewWithHostClient(&fasthttp.HostClient{\n    Addr:                upstream,\n    MaxConns:            100,\n    ReadTimeout:         5 * time.Second,\n})","handlingStrategy":"validation","validationCode":"func buildHostClient(addr string) (*fasthttp.HostClient, error) {\n    if addr == \"\" {\n        return nil, errors.New(\"upstream address required\")\n    }\n    return &fasthttp.HostClient{Addr: addr, MaxConns: 100}, nil\n}\n\nhc, err := buildHostClient(upstream)\nif err != nil || hc == nil {\n    return fmt.Errorf(\"host client setup: %w\", err)\n}\napp := client.NewWithHostClient(hc)","typeGuard":"func isHostClientReady(c *fasthttp.HostClient) bool {\n    return c != nil && c.Addr != \"\"\n}","tryCatchPattern":null,"preventionTips":["Construct the fasthttp transport inline at the call site rather than threading an uninitialized pointer.","Make factory functions that can fail return (*T, error) and always check the nil/error pair.","Add a unit test asserting NewWithHostClient is only ever called with a non-nil pointer."],"tags":["client","nil-check","constructor","fasthttp"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}