Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SDK Errors Public #1159

Merged
merged 7 commits into from
Dec 18, 2023
Merged

Make SDK Errors Public #1159

merged 7 commits into from
Dec 18, 2023

Conversation

jaxdesmarais
Copy link
Contributor

Summary of changes

  • Update all SDK errors to be public (fixes Better Error Handling #1152 and Make errors public #1080)
  • Update all errors to conform to Equatable
    • This allows for a better merchant experience as they won't need to cast to NSError and can do error comparison in a more Swift-y way (see Demo app ViewController updates as an example)

Checklist

  • Added a changelog entry

Authors

@jaxdesmarais jaxdesmarais requested a review from a team as a code owner December 12, 2023 23:06
Comment on lines +55 to +59
// MARK: - Equatable Conformance

public static func == (lhs: BTCardError, rhs: BTCardError) -> Bool {
lhs.errorCode == rhs.errorCode
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required for any of the enums with associated types. It it's able to resolve the equality of the error codes automatically.

@@ -66,7 +66,7 @@ class VenmoViewController: PaymentButtonBaseViewController {
progressBlock("Got a nonce 💎!")
completionBlock(venmoAccount)
} catch {
if (error as NSError).code == 10 {
if error as? BTVenmoError == .canceled {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🌟

Copy link
Contributor

@scannillo scannillo Dec 13, 2023

Choose a reason for hiding this comment

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

This is slick! Much improved over forcing merchants to look at our markdown list of codes and hard code the int values

Copy link
Contributor

Choose a reason for hiding this comment

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

🏈 Punt, but curious your thoughts.

For V7, we could get to something like this if error == .canceled { ... } if the error type we return is BT SDK specific. Is that what you're thinking as the V7 goal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - I think that would be dope if we return strongly typed errors. The only case it could be a little weird is we would need to wrap any errors from core. I think in most cases we do that anyways but I know there are a few instances where we return a core error directly. Though I don't think it should be a blocker for strongly typed errors.

errorCode: BTThreeDSecureError.canceled.errorCode,
completion: completionHandler
)
completionHandler(nil, BTThreeDSecureError.canceled)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This refactor pointed out a flaw in this logic. As we weren't actually sending the canceled error here and instead constructing an error similar to cancel, merchants would need to check for both the code and the raw canceled error. Instead above we can construct the NSError for the error case, but should have been sending our cancel error here. Without this updated logic we actually don't fire our cancel analytics event in BTThreeDSecureClient.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch! I actually think that resolves this ticket too (DTBTSDK-3276). I also noticed we don't have a proper unit test for the cancel case from cardinal, which I included in that ticket. Maybe we update that ticket to be unit test specific since your PR here fixes the bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah - nice! Yeah this should resolve that issue. I think we can make the ticket you created test specific and include this fix here. Is it worth calling out as a separate CHANGELOG point?

Copy link
Contributor

Choose a reason for hiding this comment

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

is there any chance we can include the test coverage work in this PR, since we're making the change now? I'm really leery about skipping tests for another day...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I poked around at testing this and it actually looks like nothing in this method is tested. Mainly because it involves testing CardinalValidationDelegate which uses a class with no inits CardinalSession. I can't figure out a good way to test this closed class CardinalSession which is required for a mock of this class. @scannillo not sure if you have any other ideas on testing this but imo it seems like it'd be a really large initiative outside of the scope of this PR.


/// 0. Invalid API client
case invalidAPIClient

static var errorDomain: String {
public static var errorDomain: String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do all the enum properties need to be public to resolve the merchant casting issue? errorDomain feels internal (but maybe it's a swifty thing).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - it needs to be public to satisfy protocol requirements since we are making the enum as a whole public.

Copy link
Contributor

Choose a reason for hiding this comment

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

we need a taylor swift emoji for "swifty" things 😄

Copy link
Contributor

@hollabaq86 hollabaq86 left a comment

Choose a reason for hiding this comment

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

Love this work! Small tweak to CHANGELOG and other general thoughts.

If we're changing code, we should incorporate test coverage into those changes, too. We absolutely can and should take the time to do the right thing:TM:. I have no problem having higher level conversations if we get pushback about this.

CHANGELOG.md Outdated Show resolved Hide resolved

/// 0. Invalid API client
case invalidAPIClient

static var errorDomain: String {
public static var errorDomain: String {
Copy link
Contributor

Choose a reason for hiding this comment

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

we need a taylor swift emoji for "swifty" things 😄

errorCode: BTThreeDSecureError.canceled.errorCode,
completion: completionHandler
)
completionHandler(nil, BTThreeDSecureError.canceled)
Copy link
Contributor

Choose a reason for hiding this comment

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

is there any chance we can include the test coverage work in this PR, since we're making the change now? I'm really leery about skipping tests for another day...

Co-authored-by: Holly Richko <[email protected]>
@jaxdesmarais
Copy link
Contributor Author

Leaving testing for a future PR as it involves wrapping a final Cardinal class without an init. None of the Cardinal delegate is tested at all so figuring out how/if we test it should be a separate workstream.

@jaxdesmarais jaxdesmarais merged commit a9bbbec into main Dec 18, 2023
7 checks passed
@jaxdesmarais jaxdesmarais deleted the make-errors-public branch December 18, 2023 16:31
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.

Better Error Handling
4 participants