{"record":{"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":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"error.go","lineNumber":104,"sourceCode":"\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":86,"sourceCodeEnd":107,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/error.go#L86-L107","documentation":"ErrFileRead is returned by DefaultCtx.SaveFileToStorage at three sites (ctx.go:582, 590, 594) when the upload exceeds the configured BodyLimit, when LimitReader.ReadFrom fails mid-stream, or when the buffered length crosses the cap. Each return wraps the filename and (where relevant) fasthttp.ErrBodyTooLarge via fmt.Errorf(\"%w: %q: %w\", ErrFileRead, fileheader.Filename, err).","triggerScenarios":"Calling c.SaveFileToStorage(fh, path, storage) when fileheader.Size already exceeds app.config.BodyLimit (ctx.go:580-583), or when the limited read returns an error (ctx.go:588-590), or when buf.Len() crosses maxUploadSize after a successful read (ctx.go:592-594). DefaultBodyLimit is used when BodyLimit is non-positive.","commonSituations":"Uploading files larger than the server's BodyLimit; clients that lie about Content-Length so the declared size is small but the stream is large; forgetting to raise BodyLimit on upload routes; reverse proxies that buffer the whole upload before forwarding.","solutions":["Raise BodyLimit on the app or route: fiber.New(fiber.Config{BodyLimit: 50 * 1024 * 1024}).","Inspect the wrapped error with errors.Is(err, fasthttp.ErrBodyTooLarge) to distinguish size violations from genuine read failures.","Validate Content-Length at the handler boundary and reject oversized uploads with 413 before reading."],"exampleFix":"// before\napp := fiber.New() // default BodyLimit ~4MiB\n// after\napp := fiber.New(fiber.Config{BodyLimit: 100 * 1024 * 1024})","handlingStrategy":"validation","validationCode":"// Reject oversized uploads at the handler boundary before reading.\nconst maxUpload = 50 << 20\nif c.Get(fiber.HeaderContentLength) != \"\" {\n    if n, _ := strconv.ParseInt(c.Get(fiber.HeaderContentLength), 10, 64); n > maxUpload {\n        return c.Status(fiber.StatusRequestEntityTooLarge).SendString(\"too large\")\n    }\n}\n// Also size the app correctly:\n// fiber.New(fiber.Config{BodyLimit: maxUpload})","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 c.Status(fiber.StatusRequestEntityTooLarge).SendString(\"upload too large\")\n        }\n        return c.Status(fiber.StatusInternalServerError).SendString(\"upload read failed\")\n    }\n    return err\n}","preventionTips":["Raise BodyLimit on upload routes to the largest expected file.","Validate Content-Length at the handler boundary and reject oversized uploads with 413.","Distinguish size violations from genuine read failures with errors.Is(err, fasthttp.ErrBodyTooLarge)."],"tags":["file","upload","body-limit","io","config"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}