[SR-14975] LocalizedError
properties get mangled by Objective-C bridge
#3364
Labels
LocalizedError
properties get mangled by Objective-C bridge
#3364
Environment
macOS 21A5284e
Xcode Version 13.0 beta 3 (13A5192i)
Swift version 5.5 (swiftlang-1300.0.24.13 clang-1300.0.25.10)
Additional Detail from JIRA
md5: f34c7dac79752ed46ed183110bcd487a
Issue Description:
So, let's say we have an enum that conforms to `LocalizedError`, which contains logic to bridge nicely to `NSError` so that Cocoa UIs will present our errors nicely:
Running that, we get:
The `userInfo` dictionary is empty, which is odd, but the `localizedDescription` does get displayed correctly after we bridge to `NSError`. So far so good.
But what if it goes through an `NSKeyedArchiver` (for example, if it gets sent over an XPC connection)?
Suddenly, the `failureReason` string is lost:
However, since `Error` contains a `_userInfo` property, we can add an implemention for that ourselves:
And that fixes it:
However: this only seems to work on error classes defined in the current module. If I try to add `_userInfo` to something defined in another module via an extension, it doesn't work:
yields:
This is not exclusive to Foundation-defined things like `POSIXError`; I've tried it with errors I've defined myself in other modules, and the behavior is the same. I can put a log in the `_userInfo` accessor, and it never gets called.
It seems to me that the expected behavior should be:
`LocalizedString` includes a default implementation for `_userInfo` that provides the various strings corresponding to the properties it supports.
We have logic in place to make sure `_userInfo` always gets propagated across when bridging to `NSError`, or at least when we're running it through an encoder.
Otherwise, the `userInfo` field is always unpopulated for bridged Swift errors, and in the end, all I have is an arbitrary integer and I don't know whether I need to get an extinguisher, an umbrella, some bug spray, or... whatever you do for frogs (a cat maybe?).
The text was updated successfully, but these errors were encountered: