{"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":164,"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":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/helpers.go#L146-L182","documentation":"Returned by the internal readContent helper when rf.ReadFrom(f) fails after the file was successfully opened. readContent streams the file into an io.ReaderFrom (e.g. a bytes.Buffer for template rendering). The error wraps the underlying read error such as an I/O failure mid-read.","triggerScenarios":"Calling c.Render() whose underlying readContent opens the template file successfully but the ReadFrom call errors — e.g. the file is truncated on disk, a concurrent process truncates it mid-read, or a disk I/O error occurs.","commonSituations":"File truncated or replaced atomically during read, disk sector errors, NFS/network filesystem hiccups, or the file being a special device that errors on read.","solutions":["Inspect the wrapped error for the specific I/O cause.","Ensure template files are not being hot-swapped while the server reads them.","For network filesystems, add retry logic or cache templates in memory at startup.","Preload and validate templates at boot rather than reading them per-request."],"exampleFix":null,"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 %q: %v\", name, err)\n        return c.Status(fiber.StatusInternalServerError).SendString(\"render error\")\n    }\n    return err\n}","preventionTips":["Do not hot-swap template files while the server reads them.","Cache parsed templates in memory at startup rather than per-request reads.","For network filesystems, copy templates locally before serving."],"tags":["filesystem","template","io","render","go"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}