{"id":"cb359d977d9a268c","repo":"gofiber/fiber","slug":"file-failed-to-read-file","errorCode":null,"errorMessage":"file: failed to read file","messagePattern":"file: failed to read file","errorType":"exception","errorClass":"ErrFileRead","httpStatus":null,"severity":"error","filePath":"error.go","lineNumber":98,"sourceCode":"\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":80,"sourceCodeEnd":102,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/error.go#L80-L102","documentation":"ErrFileRead ('file: failed to read file', error.go:99) is wrapped and returned by ctx.SaveFileToStorage (ctx.go:582,590,594) when reading the uploaded multipart file fails. Three sub-causes: the file's declared Size exceeds app.Config.BodyLimit (ctx.go:582), the actual bytes read exceed the limit (ctx.go:594, wrapping fasthttp.ErrBodyTooLarge), or the underlying reader errors mid-stream (ctx.go:590). The error is wrapped with the filename for diagnosis.","triggerScenarios":"An upload larger than BodyLimit (default or configured) passed to SaveFileToStorage; a network interruption truncating the multipart body; or a corrupt file header reporting an inconsistent size. Triggered by c.SaveFileToStorage(fh, key, storage) under those conditions.","commonSituations":"Default BodyLimit too small for legitimate uploads, reverse proxies with their own body limits truncating requests, or storage backends reporting read faults. Increasing upload size without raising BodyLimit is the classic mismatch.","solutions":["Raise app.Config.BodyLimit (or the per-route body limit) to exceed your maximum expected upload size.","Check the wrapped error (errors.Is(err, fasthttp.ErrBodyTooLarge)) to distinguish size rejection from I/O failure.","Validate Content-Length against your limit early and reject with 413 before streaming."],"exampleFix":"// before\napp := fiber.New() // default BodyLimit too small for video\n\n// after\napp := fiber.New(fiber.Config{BodyLimit: 50 * 1024 * 1024}) // 50 MB\napp.Post(\"/upload\", func(c fiber.Ctx) error {\n    fh, err := c.FormFile(\"video\")\n    if err != nil {\n        return fiber.NewError(fiber.StatusBadRequest, err.Error())\n    }\n    if err := c.SaveFileToStorage(fh, fh.Filename, storage); err != nil {\n        if errors.Is(err, fiber.ErrFileRead) {\n            return fiber.NewError(fiber.StatusRequestEntityTooLarge, \"upload too large\")\n        }\n        return err\n    }\n    return c.SendStatus(200)\n})","handlingStrategy":"try-catch","validationCode":"// Reject oversized uploads before streaming to storage.\nif fh, _ := c.FormFile(\"file\"); fh != nil && fh.Size > int64(maxUpload) {\n    return fiber.NewError(fiber.StatusRequestEntityTooLarge, \"file too large\")\n}","typeGuard":null,"tryCatchPattern":"if err := c.SaveFileToStorage(fh, key, storage); err != nil {\n    if errors.Is(err, fiber.ErrFileRead) {\n        if errors.Is(err, fasthttp.ErrBodyTooLarge) {\n            return fiber.NewError(fiber.StatusRequestEntityTooLarge, \"upload exceeds body limit\")\n        }\n        return fiber.NewError(fiber.StatusBadRequest, \"failed reading upload\")\n    }\n    return err\n}","preventionTips":["Set app.Config.BodyLimit above your maximum legitimate upload size.","Pre-check Content-Length and fh.Size against the limit before storing.","Distinguish size rejection (ErrBodyTooLarge) from I/O failure via errors.Is."],"tags":["upload","multipart","files","body-limit","fiber"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}