gofiber/fiber · critical

fasthttp.Client must not be nil

Error message

fasthttp.Client must not be nil

What it means

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.

Solutions

  1. Pass a non-nil *fasthttp.Client, or use fiber.New() to get a default one.
  2. Initialize the variable at declaration: c := &fasthttp.Client{...} instead of var c *fasthttp.Client.
  3. If the client is optional, branch on nil and use fiber.New() as fallback.

Example fix

// before
var c *fasthttp.Client
client := fiber.NewWithClient(c)

// after
client := fiber.NewWithClient(&fasthttp.Client{ReadTimeout: 5*time.Second})
Defensive patterns

Strategy: validation

Validate before calling

func newClientSafe(c *fasthttp.Client) (*fiber.Client, error) {
    if c == nil { return nil, errors.New("fasthttp.Client is nil") }
    return fiber.NewWithClient(c), nil
}

Try / catch

func safeNewWithClient(c *fasthttp.Client) (cl *fiber.Client, err error) {
    defer func() {
        if r := recover(); r != nil { err = fmt.Errorf("%v", r) }
    }()
    cl = fiber.NewWithClient(c)
    return cl, nil
}

Prevention

When it happens

Trigger: Calling fiber.NewWithClient(nil) directly, or passing a variable that was never initialized (e.g. var c *fasthttp.Client).

Common situations: 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.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/5ec1042e31a80d70. Report an issue: GitHub.

Appendix: source

Thrown at client/client.go:936

	defaultUserAgent = "fiber"
)

func init() {
	defaultClient.Store(New())
}

// New creates and returns a new Client object.
func New() *Client {
	// Follow-up performance optimizations:
	// Try to use a pool to reduce the memory allocation cost for the Fiber client and the fasthttp client.
	// If possible, also consider pooling other structs (e.g., request headers, cookies, query parameters, path parameters).
	return NewWithClient(&fasthttp.Client{})
}

// NewWithClient creates and returns a new Client object from an existing fasthttp.Client.
func NewWithClient(c *fasthttp.Client) *Client {
	if c == nil {
		panic("fasthttp.Client must not be nil")
	}
	return newClient(newStandardClientTransport(c))
}

// NewWithHostClient creates and returns a new Client object from an existing fasthttp.HostClient.
func NewWithHostClient(c *fasthttp.HostClient) *Client {
	if c == nil {
		panic("fasthttp.HostClient must not be nil")
	}
	return newClient(newHostClientTransport(c))
}

// NewWithLBClient creates and returns a new Client object from an existing fasthttp.LBClient.
func NewWithLBClient(c *fasthttp.LBClient) *Client {
	if c == nil {
		panic("fasthttp.LBClient must not be nil")
	}
	return newClient(newLBClientTransport(c))

View on GitHub (pinned to a105acad6c)