siyuan-note/siyuan · error
encode HEIF preview
Error message
encode HEIF preview: %w
What it means
This wrapped error reports that Go's image/jpeg encoder failed while writing the decoded HEIF image as a JPEG preview or thumbnail at the configured quality (previewQuality or thumbnailQuality). The underlying jpeg.Encode error is wrapped with %w so errors.Is/As work. Encoding rarely fails unless the image or buffer is in a bad state.
Solutions
- Inspect the wrapped cause with errors.Unwrap / %v of err to see the jpeg encoder's message
- Re-run the conversion; transient buffer/memory failures may resolve
- If reproducible, capture the failing asset and report it — it usually indicates a decoder/encoder compatibility bug
Example fix
// before
out, err := heif.GetOrCreate(ctx, data, opts)
if err != nil { return err }
// after
out, err := heif.GetOrCreate(ctx, data, opts)
if err != nil {
log.Errorf("HEIF conversion failed: %v", err) // wrapped jpeg.Encode cause included
return err
} Defensive patterns
Strategy: try-catch
Try / catch
result, err := heif.GetOrCreate(ctx, data, opts)
if err != nil {
if strings.Contains(err.Error(), "encode HEIF preview:") {
log.Errorf("jpeg encode failed: %v", err) // %w-preserved cause
}
return err
} Prevention
- Keep the Go image/jpeg stack healthy (adequate memory)
- Report reproducible failures with the offending asset — usually a decoder/encoder bug
- Use errors.Unwrap to log the jpeg.Encode cause
When it happens
Trigger: jpeg.Encode returns an error inside convert after successful HEIF decode — e.g. the decoded image implements an unsupported/nil image interface state or the output writer errors.
Common situations: A decoder bug returning a partially-initialized image.Image, memory pressure aborting buffer growth, a non-standard image type produced by goheic that the JPEG encoder cannot handle.
Related errors
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/eb00c621331852d4.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/heif/convert.go:146
return nil, ErrImageTooLarge
}
if err = conversionContext.Err(); err != nil {
return nil, err
}
if mode == ModeThumbnail && img.Bounds().Dx() > thumbnailWidth {
img = imaging.Resize(img, thumbnailWidth, 0, imaging.Lanczos)
}
if err = conversionContext.Err(); err != nil {
return nil, err
}
quality := previewQuality
if mode == ModeThumbnail {
quality = thumbnailQuality
}
output := bytes.NewBuffer(make([]byte, 0, estimatedJPEGSize(img)))
if err = jpeg.Encode(output, img, &jpeg.Options{Quality: quality}); err != nil {
return nil, fmt.Errorf("encode HEIF preview: %w", err)
}
if err = conversionContext.Err(); err != nil {
return nil, err
}
return output.Bytes(), nil
}
func ImageSize(source []byte) (width, height int, err error) {
if len(source) == 0 {
return 0, 0, errors.New("empty HEIF image")
}
if len(source) > MaxInputBytes {
return 0, 0, ErrInputTooLarge
}
conversionSlots <- struct{}{}
defer func() {
<-conversionSlots
}()View on GitHub (pinned to 9f775e8a12)