{"record":{"id":"eb00c621331852d4","repo":"siyuan-note/siyuan","slug":"encode-heif-preview-w","errorCode":null,"errorMessage":"encode HEIF preview: %w","messagePattern":"encode HEIF preview: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/heif/convert.go","lineNumber":146,"sourceCode":"\t\treturn nil, ErrImageTooLarge\n\t}\n\tif err = conversionContext.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\tif mode == ModeThumbnail && img.Bounds().Dx() > thumbnailWidth {\n\t\timg = imaging.Resize(img, thumbnailWidth, 0, imaging.Lanczos)\n\t}\n\tif err = conversionContext.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\n\tquality := previewQuality\n\tif mode == ModeThumbnail {\n\t\tquality = thumbnailQuality\n\t}\n\toutput := bytes.NewBuffer(make([]byte, 0, estimatedJPEGSize(img)))\n\tif err = jpeg.Encode(output, img, &jpeg.Options{Quality: quality}); err != nil {\n\t\treturn nil, fmt.Errorf(\"encode HEIF preview: %w\", err)\n\t}\n\tif err = conversionContext.Err(); err != nil {\n\t\treturn nil, err\n\t}\n\treturn output.Bytes(), nil\n}\n\nfunc ImageSize(source []byte) (width, height int, err error) {\n\tif len(source) == 0 {\n\t\treturn 0, 0, errors.New(\"empty HEIF image\")\n\t}\n\tif len(source) > MaxInputBytes {\n\t\treturn 0, 0, ErrInputTooLarge\n\t}\n\tconversionSlots <- struct{}{}\n\tdefer func() {\n\t\t<-conversionSlots\n\t}()","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/heif/convert.go#L128-L164","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nout, err := heif.GetOrCreate(ctx, data, opts)\nif err != nil { return err }\n// after\nout, err := heif.GetOrCreate(ctx, data, opts)\nif err != nil {\n\tlog.Errorf(\"HEIF conversion failed: %v\", err) // wrapped jpeg.Encode cause included\n\treturn err\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"result, err := heif.GetOrCreate(ctx, data, opts)\nif err != nil {\n\tif strings.Contains(err.Error(), \"encode HEIF preview:\") {\n\t\tlog.Errorf(\"jpeg encode failed: %v\", err) // %w-preserved cause\n\t}\n\treturn err\n}","preventionTips":["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"],"tags":["heif","jpeg","encoding"],"backgroundTag":"image-encode-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}