{"id":"010f7c2657193c57","repo":"gofiber/fiber","slug":"file-file-header-is-nil","errorCode":null,"errorMessage":"file: file header is nil","messagePattern":"file: file header is nil","errorType":"exception","errorClass":"ErrFileHeaderNil","httpStatus":null,"severity":"error","filePath":"error.go","lineNumber":97,"sourceCode":"\n\t// SyntaxError is a description of a JSON syntax error.\n\tSyntaxError = json.SyntaxError\n\n\t// UnmarshalTypeError describes a JSON value that was\n\t// not appropriate for a value of a specific Go type.\n\tUnmarshalTypeError = json.UnmarshalTypeError\n\n\t// UnsupportedTypeError is returned by Marshal when attempting\n\t// to encode an unsupported value type.\n\tUnsupportedTypeError = json.UnsupportedTypeError\n\n\t// UnsupportedValueError exposes json.UnsupportedValueError to describe unsupported values encountered during encoding.\n\tUnsupportedValueError = json.UnsupportedValueError\n)\n\n// File errors\nvar (\n\tErrFileHeaderNil = errors.New(\"file: file header is nil\")\n\tErrFileOpen      = errors.New(\"file: failed to open file\")\n\tErrFileRead      = errors.New(\"file: failed to read file\")\n\tErrFileStore     = errors.New(\"file: failed to store file\")\n)\n","sourceCodeStart":79,"sourceCodeEnd":102,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/error.go#L79-L102","documentation":"ErrFileHeaderNil (error.go:97) is returned by ctx.SaveFile (ctx.go:559) and ctx.SaveFileToStorage (ctx.go:567) when the supplied *multipart.FileHeader is nil. Both methods dereference the header (for filename, Open(), Size), so a nil guard runs first and returns this sentinel instead of panicking on a nil pointer dereference.","triggerScenarios":"Calling c.SaveFile(nil, path), or passing a FileHeader obtained from c.FormFile(\"field\") when the field was absent (and the caller didn't check the error), then forwarding that nil header to SaveFile. Also when looping over an empty file slice and dereferencing.","commonSituations":"Upload handlers that assume a file is always present, form fields renamed client-side, or multipart parsing that returns a nil header on missing parts.","solutions":["Check that the file header is non-nil and that c.FormFile returned no error before saving.","Validate file presence and return a 400 to the client when the upload field is missing.","Guard the save call: if fileheader == nil { return fiber.NewError(fiber.StatusBadRequest, \"file required\") }."],"exampleFix":"// before\nfh, _ := c.FormFile(\"upload\")\nc.SaveFile(fh, \"./uploads/\"+fh.Filename) // panics if fh is nil\n\n// after\nfh, err := c.FormFile(\"upload\")\nif err != nil {\n    return fiber.NewError(fiber.StatusBadRequest, \"upload field required\")\n}\nreturn c.SaveFile(fh, \"./uploads/\"+fh.Filename)","handlingStrategy":"validation","validationCode":"// Confirm the file part exists before saving.\nfh, err := c.FormFile(\"upload\")\nif err != nil || fh == nil {\n    return fiber.NewError(fiber.StatusBadRequest, \"upload file required\")\n}\nreturn c.SaveFile(fh, dest)","typeGuard":"// narrow a possibly-nil header before dereferencing\nfunc validHeader(fh *multipart.FileHeader) (*multipart.FileHeader, error) {\n    if fh == nil {\n        return nil, fiber.ErrFileHeaderNil\n    }\n    return fh, nil\n}","tryCatchPattern":"if err := c.SaveFile(fh, dest); err != nil {\n    if errors.Is(err, fiber.ErrFileHeaderNil) {\n        return fiber.NewError(fiber.StatusBadRequest, \"no file provided\")\n    }\n    return err\n}","preventionTips":["Always check the error from c.FormFile and the nil-ness of the header.","Return a 400 when the expected upload field is missing.","Never index into a file slice without a bounds/nil check."],"tags":["upload","multipart","files","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}