kataras/iris · error

asset: read file: %w

Error message

asset: read file: %w

What it means

The internal asset helper reads a single file from an fs.FS and wraps any fs.ReadFile failure as 'asset: read file: <err>'. It is used when fetching embedded or on-disk view/asset files, giving callers a clear point of failure with the underlying OS/FS error preserved via %w.

Source

Thrown at view/fs.go:56

		if info.IsDir() {
			return nil
		}

		walkFnErr := walkFn(path, info, err)
		if walkFnErr != nil {
			return fmt.Errorf("walk: walkFn: %w", walkFnErr)
		}

		return nil
	})

}

func asset(fileSystem fs.FS, name string) ([]byte, error) {
	data, err := fs.ReadFile(fileSystem, name)
	if err != nil {
		return nil, fmt.Errorf("asset: read file: %w", err)
	}

	return data, nil
}

func getFS(fsOrDir any) fs.FS {
	return context.ResolveFS(fsOrDir)
}

func getRootDirName(fileSystem fs.FS) string {
	rootDirFile, err := fileSystem.Open(".")
	if err == nil {
		rootDirStat, err := rootDirFile.Stat()
		if err == nil {
			return rootDirStat.Name()
		}
	}

View on GitHub (pinned to 7bedaf55a0)

Solutions

  1. Use the exact relative, slash-separated path with no leading slash (e.g. 'views/index.html').
  2. Verify the file is present in the FS/embed directive (embed must list it via //go:embed pattern).
  3. Check the wrapped error: os.ErrNotExist means wrong name; EACCES means permissions.
  4. In callers, guard with fs.Stat or errors.Is(err, fs.ErrNotExist) and fall back to a default asset.

Example fix

// before
asset(fsys, "/views/index.html") // leading slash invalid in io/fs
// after
asset(fsys, "views/index.html")
Defensive patterns

Strategy: fallback

Validate before calling

name = strings.TrimPrefix(name, "/") // io/fs requires clean relative paths
if _, err := fs.Stat(fileSystem, name); err != nil {
    return fallbackAsset(name)
}

Type guard

func assetExists(fsys fs.FS, name string) bool {
    _, err := fs.Stat(fsys, name)
    return err == nil
}

Try / catch

data, err := asset(fsys, name)
if err != nil {
    if errors.Is(err, fs.ErrNotExist) {
        return defaultAsset, nil // serve fallback
    }
    return nil, fmt.Errorf("asset %s: %w", name, err)
}

Prevention

When it happens

Trigger: Get (or an anonymous loader using asset) requests a name that does not exist in the filesystem, is a directory, or cannot be opened — fs.ReadFile returns ErrNotExist/permission error and it is wrapped here.

Common situations: Requesting a template path with wrong extension or leading slash (io/fs requires clean, slash-separated, no-leading-slash names), asset not included in embed.FS, or file deleted from a deployed image.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of kataras/iris@7bedaf55a0 (2026-08-30). Data as JSON: /api/errors/9b42cdb361e42110. Report an issue: GitHub.