Skip to content

CustomerInfoResponseHandler: return CustomerInfo instead of error if the response was successful#1778

Merged
NachoSoto merged 2 commits into
mainfrom
subscriber-attributes-error-
Jul 29, 2022
Merged

CustomerInfoResponseHandler: return CustomerInfo instead of error if the response was successful#1778
NachoSoto merged 2 commits into
mainfrom
subscriber-attributes-error-

Conversation

@NachoSoto

@NachoSoto NachoSoto commented Jul 27, 2022

Copy link
Copy Markdown
Contributor

Fixes SDKONCALL-62.
The behavior in version 3.x was that we would return a CustomerInfo AND the attribute errors. The test in that version however only verified that the error was returned.

When we converted (Value, Error) callbacks to using Result (#1387), I assumed this was an error. Because of that incomplete test, we didn't notice this wasn't the desired behavior.

If the backend returns a 200, it's assumed that the errors aren't more important than the correct CustomerInfo.

The new implementation simply logs the attribute errors (which is now tested), and returns the CustomerInfo:

😿‼️ One or more of the attributes sent could not be saved. ["$email": "email is not in valid format"]

@NachoSoto NachoSoto added bug pr:fix A bug fix labels Jul 27, 2022
@NachoSoto NachoSoto requested a review from a team July 27, 2022 20:31
@NachoSoto NachoSoto force-pushed the subscriber-attributes-error- branch from 6294bb3 to dfb5bb4 Compare July 27, 2022 20:40
@NachoSoto NachoSoto force-pushed the error-logging-message branch from 8bf0e45 to 5582a89 Compare July 27, 2022 21:01
@NachoSoto NachoSoto force-pushed the subscriber-attributes-error- branch from dfb5bb4 to 88d2c06 Compare July 27, 2022 21:01
Base automatically changed from error-logging-message to main July 28, 2022 15:11
… if the response was successful

Fixes [SDKONCALL-62].
The behavior in version 3.x was that we would return a `CustomerInfo` AND the attribute errors. [The test in that version](https://github.com/RevenueCat/purchases-ios/blob/3.14.2/PurchasesTests/SubscriberAttributes/BackendSubscriberAttributesTests.swift#L370-L404) however only verified that the error was returned.

When we converted `(Value, Error)` callbacks to using `Result` (#1387), I assumed this was an error. Because of that incomplete test, we didn't notice this wasn't the desired behavior.

If the backend returns a 200, it's assumed that the errors aren't more important than the correct `CustomerInfo`.

The new implementation simply logs the attribute errors (which is now tested), and returns the `CustomerInfo`.
@NachoSoto NachoSoto force-pushed the subscriber-attributes-error- branch from 88d2c06 to 112d636 Compare July 28, 2022 15:12

@tonidero tonidero 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.

LGTM!


var loggedMessages = [String]()
let originalLogHandler = Logger.logHandler
defer { Logger.logHandler = originalLogHandler }

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.

😮 Didn't know about this. Interesting!

return ErrorUtils.backendError(
withBackendCode: self.code,
backendMessage: self.message,
message: self.attributeErrors.description,

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.

Hmm are we still using the backendMessage parameter anywhere? I couldn't find a usage of it but I might be wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh good catch! I changed this before I fixed #1776 and forgot to add it back. Thanks for the thorough review 👏🏻

@@ -0,0 +1,29 @@
{
"headers" : {

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.

Just to make sure, these files are snapshots of the requests done during the unit tests. Then the tests check that the request remains the same, correct? This is nice!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup exactly :) without needing to check each thing individually.

@NachoSoto NachoSoto merged commit e58a00b into main Jul 29, 2022
@NachoSoto NachoSoto deleted the subscriber-attributes-error- branch July 29, 2022 17:04

@taquitos taquitos 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.

Ugh, this review was sitting in draft.

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.

I think we should make a new API so this error logging is explicit

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree. I didn't feel too bad about this instance because it's covered by a test, so removing it would make it fail.
I don't love the implicit logging, but I think explicit logging of errors everywhere would be even worse :/ so I'm not sure how to improve this at the moment.

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.

Yeah, that's a great point, as long as it's covered by a test, this is much more palatable 😄

@NachoSoto NachoSoto mentioned this pull request Jul 29, 2022
This was referenced Aug 1, 2022
NachoSoto added a commit that referenced this pull request Aug 1, 2022
### Fixes:
* `CustomerInfoResponseHandler`: return `CustomerInfo` instead of error if the response was successful (#1778) via NachoSoto (@NachoSoto)
* Error logging: `logErrorIfNeeded` no longer prints message if it's the same as the error description (#1776) via NachoSoto (@NachoSoto)
* fix another broken link in docC docs (#1777) via aboedo (@aboedo)
* fix links to restorePurchase (#1775) via aboedo (@aboedo)
* fix getProducts docs broken link (#1772) via aboedo (@aboedo)

### Improvements:
* `Logger`: wrap `message` in `@autoclosure` to avoid creating when `LogLevel` is disabled (#1781) via NachoSoto (@NachoSoto)

### Other changes:
* Lint: fixed `SubscriberAttributesManager` (#1774) via NachoSoto (@NachoSoto)
NachoSoto added a commit that referenced this pull request Aug 17, 2022
Follow up to #1776. That PR added a lot of tests to cover error logging, and #1778 introduced a small error that wasn't covered: if attribute errors are empty, the `localizedDescription` could end up becoming `"[:]"`. This fixes that.

## Examples:

This was the underlying error returned (the output of `error.localizedDescription`):

#### Before:
> [:]
#### After:
> There was a credentials issue. Check the underlying error for more details.

And this is what was logged (notice that I also fixed `ErrorCode.invalidCredentialsError` to not log as an "Apple Error"):

#### Before:
> 🍎‼️There was a credentials issue. Check the underlying error for more details. [:]
#### After:
> 😿‼️ There was a credentials issue. Check the underlying error for more details.
NachoSoto added a commit that referenced this pull request Aug 17, 2022
Follow up to #1776. That PR added a lot of tests to cover error logging, and #1778 introduced a small error that wasn't covered: if attribute errors are empty, the `localizedDescription` could end up becoming `"[:]"`. This fixes that.

## Examples:

This was the underlying error returned (the output of `error.localizedDescription`):

#### Before:
> [:]
#### After:
> There was a credentials issue. Check the underlying error for more details.

And this is what was logged (notice that I also fixed `ErrorCode.invalidCredentialsError` to not log as an "Apple Error"):

#### Before:
> 🍎‼️There was a credentials issue. Check the underlying error for more details. [:]
#### After:
> 😿‼️ There was a credentials issue. Check the underlying error for more details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug pr:fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants