{"record":{"id":"1bdfd334862e8958","repo":"gofiber/fiber","slug":"failed-to-read-w","errorCode":null,"errorMessage":"failed to read: %w","messagePattern":"failed to read: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helpers.go","lineNumber":167,"sourceCode":"\n\treturn cfg\n}\n\n// readContent opens a named file and read content from it\nfunc readContent(rf io.ReaderFrom, name string) (int64, error) {\n\t// Read file\n\tf, err := os.Open(filepath.Clean(name))\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to open: %w\", err)\n\t}\n\tdefer func() {\n\t\tif err = f.Close(); err != nil {\n\t\t\tlog.Errorf(\"Error closing file: %s\", err)\n\t\t}\n\t}()\n\tn, readErr := rf.ReadFrom(f)\n\tif readErr != nil {\n\t\treturn n, fmt.Errorf(\"failed to read: %w\", readErr)\n\t}\n\treturn n, nil\n}\n\n// quoteEscapeMask marks the lanes of w holding bytes quoteRawString must\n// escape: '\\\\', '\"', any C0 control (including HTAB), or DEL. Lanes >= 0x80\n// are never marked; non-ASCII bytes pass through verbatim. This is\n// utils.IndexNonQuotable's RFC 9110 set widened by HTAB, which the RFC\n// permits as qdtext but this function has always percent-encoded.\nfunc quoteEscapeMask(w uint64) uint64 {\n\treturn swar.MatchByteMask(w, '\\\\') | swar.MatchByteMask(w, '\"') |\n\t\tswar.MatchRangeMask(w, 0x00, 0x1f) | swar.MatchByteMask(w, 0x7f)\n}\n\n// indexQuoteEscape returns the index of the first byte quoteEscapeMask\n// matches, or -1 if raw needs no escaping. It scans eight bytes at a time,\n// finishing inputs of 8+ bytes with one overlapping word; shorter inputs\n// are checked byte-wise.","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/helpers.go#L149-L185","documentation":"Returned by readContent when rf.ReadFrom fails after the file was successfully opened. Wraps the underlying read error: 'failed to read: <err>'. Used by Ctx.Render's raw-template path (res.go:938). Distinct from the open failure: here the open succeeded but reading bytes errored.","triggerScenarios":"An I/O error occurs while reading the template file: the file is truncated mid-read, the storage is interrupted, the file is a special device that errors on read, or (rarely) the filesystem fails after open.","commonSituations":"Corrupted template file on disk, network filesystem hiccup after open, concurrent truncation of the file mid-read, or a path that resolves to a non-regular file.","solutions":["Log the wrapped underlying error to identify whether it is a transient I/O or a media failure.","Embed templates (go:embed) so reads come from the binary, eliminating filesystem I/O errors entirely.","If reading from a network mount, add retry-on-read-error logic or fall back to a cached copy.","Validate file integrity (size, checksum) after deploy so truncated templates are caught before serving."],"exampleFix":"// before: templates read from disk at request time\nreturn c.Render(\"templates/home.tmpl\", bind)\n\n// after: templates embedded into the binary; no runtime read\n//go:embed templates/*\nvar tmplFS embed.FS","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"if err := c.Render(name, bind); err != nil {\n    if strings.Contains(err.Error(), \"failed to read\") {\n        log.Errorf(\"template read error for %s: %v\", name, err)\n        return fiber.NewError(fiber.StatusInternalServerError)\n    }\n    return err\n}","preventionTips":["Embed templates (go:embed) so reads come from the binary, removing runtime I/O failures.","Validate template integrity (size/checksum) after deploy to catch truncation.","For network-mounted templates, cache or retry on read errors.","Distinguish 'failed to open' (missing) from 'failed to read' (I/O mid-file) when logging."],"tags":["template","filesystem","render","gofiber","io"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}