Fixed ErrorCode's domain in Swift#1275
Merged
Merged
Conversation
In version 3, `Objective-C` had the constant `RCPurchasesErrorDomain` (`Purchases.ErrorDomain` in Swift), manually defined in Objective-C. This was also visible to Swift through the constant of the same name, and both had the value `RCPurchasesErrorDomain`. In version 4, written in Swift, it's not possible to expose global constants to Objective-C. The way the Swift compiler exposes error domains is through the definition of the `Error` itself. In our case, it's `ErrorCode`, which has an `@objc` name `RCPurchasesErrorCode`. The compiler appends the word "Domain" to that, creating the new `RCPurchasesErrorCodeDomain` which superseded `RCPurchasesErrorDomain`. However, prior to this change, our implementation of `CustomNSError` was not overriding `errorDomain`, so the value of these constants was the default `RevenueCat.ErrorCode` which was a change (regression) from version 3.0. Unfortunately because of a bug (https://bugs.swift.org/browse/SR-15841), this isn't enough to fix the domain when `ErrorCode` is converted to `NSError`. This PR includes a workaround, but unfortunately the workaround only provides the correct value to Swift. `RCPurchasesErrorCodeDomain` will still be the "invalid" `RevenueCat.ErrorCode`, but this is better than nothing. At least now we have an explicit domain value for Swift errors, and we're exposing a constant for them (`Purchases.ErrorDomain` which was already available in version 3, so it's back now).
f07ab08 to
06eb358
Compare
aboedo
approved these changes
Feb 9, 2022
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.

Background
In version 3,
Objective-Chad the constantRCPurchasesErrorDomain(Purchases.ErrorDomainin Swift), manually defined in Objective-C.This was also visible to Swift through the constant of the same name, and both had the value
RCPurchasesErrorDomain.In version 4, written in Swift, it's not possible to expose global constants to Objective-C. The way the Swift compiler exposes error domains is through the definition of the
Erroritself.In our case, it's
ErrorCode, which has an@objcnameRCPurchasesErrorCode. The compiler appends the word "Domain" to that, creating the newRCPurchasesErrorCodeDomainwhich supersededRCPurchasesErrorDomain.However, prior to this change, our implementation of
CustomNSErrorwas not overridingerrorDomain, so the value of these constants was the defaultRevenueCat.ErrorCodewhich was a change (regression) from version 3.x.Unfortunately because of a bug (https://bugs.swift.org/browse/SR-15841), this isn't enough to fix the domain when
ErrorCodeis converted toNSError. This PR includes a workaround, but unfortunately the workaround only provides the correct value to Swift.RCPurchasesErrorCodeDomainwill still be the "invalid"RevenueCat.ErrorCode, but this is better than nothing.At least now we have an explicit domain value for Swift errors, and we're exposing a constant for them (
Purchases.ErrorDomainwhich was already available in version 3, so it's back now).TLDR:
In version 4 we had lost
Purchases.ErrorDomain(which is arguably still useful to Swift), and the value was the auto-generatedRevenueCat.ErrorCodeinstead of the one in version 3RCPurchasesErrorDomain.Swift now has a new custom domain
RevenueCatErrorDomainwhich matches Apple's domains likeNSCocoaErrorDomain.