siyuan-note/siyuan · error
ErrInvalidMode
ErrInvalidMode
Error message
invalid HEIF conversion mode
What it means
ErrInvalidMode means Options.Mode passed to GetOrCreate is neither ModePreview ("preview") nor ModeThumbnail ("thumb"). GetOrCreate validates all options (mode, BoxID for encrypted input) before spending the single conversion slot or decoding, so invalid options fail fast and cheaply. The zero-value Options{} has an empty Mode, so forgetting to set Mode always triggers this.
Solutions
- Set options.Mode explicitly to heif.ModePreview or heif.ModeThumbnail
- Use the typed constants (heif.Mode) instead of raw strings to get compile-time safety
- Validate options in your own wrapper before calling GetOrCreate
Example fix
// before
result, err := heif.GetOrCreate(ctx, data, heif.Options{})
// after
result, err := heif.GetOrCreate(ctx, data, heif.Options{Mode: heif.ModePreview}) Defensive patterns
Strategy: validation
Validate before calling
if opts.Mode != heif.ModePreview && opts.Mode != heif.ModeThumbnail {
return fmt.Errorf("mode must be preview or thumb")
} Try / catch
if err != nil {
if errors.Is(err, heif.ErrInvalidMode) {
// fix Options construction — Mode was empty or invalid
}
return err
} Prevention
- Always set Options.Mode using the typed heif.Mode constants
- Never rely on the zero value of Options
- Validate encrypted options (BoxID) together with Mode before calling
When it happens
Trigger: Calling GetOrCreate with Options{} (zero value) or any Mode other than ModePreview/ModeThumbnail, as guarded at cache.go:89 and covered by TestGetOrCreateValidatesOptionsBeforeDecode.
Common situations: Constructing Options without initializing Mode (empty string), a typo'd custom mode constant, refactoring that renamed modes without updating a caller.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- cannot save incomplete notebook crypto configuration
- Conf.Language(249)
- empty HEIF image
- empty HEIF image
- encrypted HEIF cache requires a notebook ID
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/15ebc1376a07096d.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/heif/convert.go:54
previewQuality = 90
thumbnailQuality = 85
MaxInputBytes = 32 * 1024 * 1024
maxDimension = 65535
desktopMaxPixels = 50_000_000
mobileMaxPixels = 12_500_000
// 解码层仅接受至多 10-bit 4:2:0、无透明通道的单 slice 图像;这里按解码面、CTU 表、
// NRGBA 和一次合并变换预留每像素 16 字节,再单独预留源文件、RBSP 副本与 JPEG 输出。
workingBytesPerPixel = 16
desktopWorkingBudget = 960 * 1024 * 1024
mobileWorkingBudget = 320 * 1024 * 1024
desktopOutputReserve = 96 * 1024 * 1024
mobileOutputReserve = 32 * 1024 * 1024
)
var (
ErrInputTooLarge = errors.New("HEIF image exceeds the input size limit")
ErrImageTooLarge = errors.New("HEIF image exceeds the dimension limit")
ErrInvalidMode = errors.New("invalid HEIF conversion mode")
conversionSlots = make(chan struct{}, 1)
maxPixels = platformMaxPixels()
)
func platformMaxPixels() int {
if runtime.GOOS == "android" || runtime.GOOS == "ios" {
return pixelsWithinBudget(mobileWorkingBudget, mobileOutputReserve, mobileMaxPixels)
}
return pixelsWithinBudget(desktopWorkingBudget, desktopOutputReserve, desktopMaxPixels)
}
func ReadFileLimited(path string, maxBytes int64) ([]byte, error) {
if maxBytes <= 0 {
return nil, ErrInputTooLarge
}
file, err := os.Open(path)
if err != nil {View on GitHub (pinned to 9f775e8a12)