Skip to content

fix(ios): reject the promise instead of raising NSException in image compression - #411

Draft
jayeshbjain wants to merge 1 commit into
numandev1:mainfrom
jayeshbjain:fix/ios-image-compress-reject-instead-of-raise
Draft

jayeshbjain wants to merge 1 commit into
numandev1:mainfrom
jayeshbjain:fix/ios-image-compress-reject-instead-of-raise

Conversation

@jayeshbjain

Copy link
Copy Markdown

Problem

The iOS image-compression path raises NSException on several failure conditions (unsupported_value ×2, file_error, drawing_error). An NSException raised inside a TurboModule invocation is not catchable from JavaScript and aborts the whole app (SIGABRT) — Swift do/catch can't intercept ObjC exceptions either, so the existing catch in ImageMain.image_compress never fires.

This is not theoretical: we hit it in production. iOS relocated our app's data container during an OS update (Apple documents that container paths may change between updates), which invalidated a stored absolute file path. Image.compress on that dead path → loadImage returns nil → NSException("unsupported_value") → the app died 991ms after every launch, a permanent boot loop for the user (crash log frames: ImageCompressor.manualCompressHandler ← getAbsoluteImagePath ← Compressor.image_compress).

Fix

Convert all four raise sites to Swift throw NSError(...) and propagate:

  • manualCompressHandler / autoCompressHandler / manualCompress / writeImage / manualResize become throws
  • ImageMain.image_compress: the base64 branch's errors flow to the existing outer catch; the URI branch runs inside getAbsoluteImagePath's escaping closure (outside the outer do/catch), so it gets its own do/catch that calls reject
  • Error domains/messages preserved (unsupported_value / "Unsupported value type." etc.), so callers that match on the message keep working — they now receive a promise rejection instead of a crash

Also removed in passing: free(&targetData) before the drawing_error raise — targetData is a Swift-managed [UInt8] array, so calling free() on its address was undefined behavior; the array is released by ARC.

Behavior change

None on success paths. On the four failure conditions the JS promise rejects (with the same error name/message) instead of the process aborting.

Testing

  • Shipping in our production app as a patch-package patch of 1.19.2 (same change rebased onto main here).
  • Repro for the original crash: call Image.compress('file:///path/that/does/not/exist.jpg', { compressionMethod: 'manual' }) on iOS — before: SIGABRT; after: promise rejection.

Marked as draft for maintainer review of error-domain conventions.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Native iOS failure paths were not runtime-verified, and the file-write error message conflicts with the stated compatibility guarantee.

1 open issue
What changed in this PR

This PR changes iOS image-compression failures from process-aborting exceptions to Swift errors that reject the JavaScript promise.

Changes:

  • Replaces four NSException raise sites with thrown errors and propagates them through compression helpers.
  • Adds rejection handling inside the escaping URI completion closure.
  • Removes an invalid free() call on Swift-managed array storage.
File Description
ios/​Image/​ImageMain.swift Propagates compression errors and rejects failures inside the URI callback.
ios/​Image/​ImageCompressor.swift Converts exceptions to thrown errors and removes unsafe memory cleanup.

Review effort: Balanced

// TurboModule invocation aborts the whole app and no JS catch
// can intercept it — the caller rejects the promise instead.
throw NSError(domain: "file_error", code: 1,
userInfo: [NSLocalizedDescriptionKey: "Error writing file: \(error.localizedDescription)"])
@numandev1

Copy link
Copy Markdown
Owner

@jayeshbjain can you fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants