fix(image): surface underlying error when image optimization fails#6
Open
fix(image): surface underlying error when image optimization fails#6
Conversation
When all size/quality combinations in optimizeImageToJpeg or optimizeImageToPng fail (e.g. because sharp is not installed), the thrown error now includes the message and cause from the last resize attempt instead of a generic opaque string. Adds regression tests for the JPEG path. Closes openclaw#73148
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When
sharpis not installed (common on Linux global installs or slim Docker images) everyresizeToJpeg/resizeToPngcall inside the optimization loops throwsMODULE_NOT_FOUND. The loop silently swallows all 25 (JPEG) or 20 (PNG) errors via an emptycatch {}, so the final throw surfaces only a generic"Failed to optimize image"with no diagnostic information. Users have no way to tell whether the failure is a missing native dependency, an unsupported format, or a memory limit.Root cause
optimizeImageToJpeginsrc/web/media.ts(lines ~454-477) andoptimizeImageToPnginsrc/media/image-ops.ts(lines ~429-454) both had:The catch block discarded the error entirely and the terminal throw provided no context.
Fix
Capture the last loop error in
lastResizeErr. After the loop, include its message in the thrown error and attach it ascause:This is minimal -- no behavior change for successful paths, and the
smallestfallback return is still reached before the throw when any combination succeeds.Test plan
src/web/media.test.ts(image optimization error surfacingdescribe block): mockresizeToJpegto always reject with a specific Error, verify the caught error message includes the underlying cause, verify.causeis set, verify non-Error thrown values fall back to the generic message.src/web/media.test.tspass (vitest run src/web/media.test.ts)Closes openclaw#73148
Generated by Claude Code