ReviewOS

also looking at this

craft-native/craft

fix(ios): preserve specific protocol errors during dispatch

#303
Merged glennmichael123 wants to merge codex/preserve-native-bridge-errors into main
3 files +51 -11
packages/zig/src/bridge_error.zigmodified+13-11
Changes to packages/zig/src/bridge_error.zig
@@ -194,17 +194,16 @@ pub fn errorMessage(err: BridgeError) []const u8 {
194194/// the dispatcher exposes. Errors the protocol cannot name become the honest
195195/// catch-all instead of leaving the page without a reply.
196196pub fn fromHandlerError(err: anyerror) BridgeError {
197 // Preserve every explicitly declared protocol error before translating
198 // implementation aliases. A hand-maintained subset loses useful errors
199 // (for example WebViewHandleNotSet) when new handlers start returning them.
200 inline for (@typeInfo(BridgeError).error_set.error_names.?) |name| {
201 const protocol_error = @field(BridgeError, name);
202 if (err == protocol_error) return protocol_error;
203 }
197204 return switch (err) {
198 error.AllocationFailed, error.OutOfMemory => BridgeError.AllocationFailed,
199 error.UnknownAction => BridgeError.UnknownAction,
200 error.MissingData => BridgeError.MissingData,
201 error.InvalidJSON => BridgeError.InvalidJSON,
202 error.InvalidParameter => BridgeError.InvalidParameter,
203 error.Busy => BridgeError.Busy,
204 error.PlatformNotSupported, error.UnsupportedPlatform => BridgeError.PlatformNotSupported,
205 error.NotFound => BridgeError.NotFound,
206 error.PermissionDenied => BridgeError.PermissionDenied,
207 error.Timeout => BridgeError.Timeout,
205 error.OutOfMemory => BridgeError.AllocationFailed,
206 error.UnsupportedPlatform => BridgeError.PlatformNotSupported,
208207 else => BridgeError.NativeCallFailed,
209208 };
210209}
@@ -379,7 +378,10 @@ test "errorCodeString returns correct codes" {
379378
380379test "handler error narrowing preserves protocol errors" {
381380 const testing = std.testing;
382 try testing.expectEqual(BridgeError.Busy, fromHandlerError(error.Busy));
381 inline for (@typeInfo(BridgeError).error_set.error_names.?) |name| {
382 const protocol_error = @field(BridgeError, name);
383 try testing.expectEqual(protocol_error, fromHandlerError(protocol_error));
384 }
383385 try testing.expectEqual(BridgeError.PlatformNotSupported, fromHandlerError(error.UnsupportedPlatform));
384386 try testing.expectEqual(BridgeError.AllocationFailed, fromHandlerError(error.OutOfMemory));
385387 try testing.expectEqual(BridgeError.NativeCallFailed, fromHandlerError(error.NotInTheProtocol));