Skip to content

Codable: debug log entire JSON when decoding fails#2864

Merged
NachoSoto merged 1 commit into
mainfrom
codable-log-whole-json
Jul 24, 2023
Merged

Codable: debug log entire JSON when decoding fails#2864
NachoSoto merged 1 commit into
mainfrom
codable-log-whole-json

Conversation

@NachoSoto

Copy link
Copy Markdown
Contributor

This is a simple example, but on a larger JSON it becomes easier for us to debug decoding the entire JSON rather than seeing only the part that failed:

[codable] DEBUG: ℹ️ Encountered error when decoding JSON: {"e": "e3"}
[codable] ERROR: 😿‼️ Couldn't decode data from json, it was corrupted: Context(codingPath: [CodingKeys(stringValue: "e", intValue: nil)], debugDescription: "Cannot initialize E from invalid String value e3", underlyingError: nil)

This is a simple example, but on a larger JSON it becomes easier for us to debug decoding the entire JSON rather than seeing only the part that failed:
```
[codable] DEBUG: ℹ️ Encountered error when decoding JSON: {"e": "e3"}
[codable] ERROR: 😿‼️ Couldn't decode data from json, it was corrupted: Context(codingPath: [CodingKeys(stringValue: "e", intValue: nil)], debugDescription: "Cannot initialize E from invalid String value e3", underlyingError: nil)
```
@NachoSoto NachoSoto requested a review from a team July 24, 2023 09:25
@NachoSoto

Copy link
Copy Markdown
Contributor Author

I love these little QoL improvements. I was trying to figure out what was wrong in a test, this helped.

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

Left a comment... But can see it working either way

static func logDecodingError(_ error: Error, type: Any.Type) {
static func logDecodingError(_ error: Error, type: Any.Type, data: Data? = nil) {
if let data = data {
Logger.debug(Strings.codable.invalid_data_when_decoding(data))

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 so this will be a separate log. I wonder if it should be part of the error log below...

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 did it this way for 2 reasons, let me know what you think:

  • The implementation of this method uses like 5 different strings so I'd have to modify all of them
  • The JSON might contain sensitive data, so we might not want the whole thing to be in the console. In release builds, debug logs aren't enabled by default, therefore eliminating this.

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.

Ah ok makes sense 👍

@NachoSoto NachoSoto enabled auto-merge (squash) July 24, 2023 11:28
@NachoSoto NachoSoto merged commit bb3a974 into main Jul 24, 2023
@NachoSoto NachoSoto deleted the codable-log-whole-json branch July 24, 2023 11:39
NachoSoto added a commit that referenced this pull request Jul 26, 2023
**This is an automatic release.**

_This release is compatible with Xcode 15 beta 5 and visionOS beta 2_

### Bugfixes
* `xrOS`: fixed `SubscriptionStoreView` for visionOS beta 2 (#2884) via
Josh Holtz (@joshdholtz)
### Performance Improvements
* `Perf`: update `CustomerInfo` cache before anything else (#2865) via
NachoSoto (@NachoSoto)
### Other Changes
* `SimpleApp`: added support for localization (#2880) via NachoSoto
(@NachoSoto)
* `TestStoreProduct`: made available on release builds (#2861) via
NachoSoto (@NachoSoto)
* `Tests`: increased default logger capacity (#2870) via NachoSoto
(@NachoSoto)
* `CustomEntitlementComputation`: removed `invalidateCustomerInfoCache`
(#2866) via NachoSoto (@NachoSoto)
* `SimpleApp`: updates for TestFlight compatibility (#2862) via
NachoSoto (@NachoSoto)
* `BasePurchasesTests`: consolidate to only initialize one `DeviceCache`
(#2863) via NachoSoto (@NachoSoto)
* `Codable`: debug log entire JSON when decoding fails (#2864) via
NachoSoto (@NachoSoto)
* `IntegrationTests`: replaced `Purchases.shared` with a `throw`ing
property (#2867) via NachoSoto (@NachoSoto)
* `NetworkError`: 2 new tests to ensure underlying error is included in
description (#2843) via NachoSoto (@NachoSoto)
* Add SPM `Package.resolved` for Xcode Cloud (#2844) via NachoSoto
(@NachoSoto)
* `CustomEntitlementComputation`: added integration test for
cancellations (#2849) via NachoSoto (@NachoSoto)
* `CustomEntitlementComputation`: removed
`syncPurchases`/`restorePurchases` (#2854) via NachoSoto (@NachoSoto)

---------

Co-authored-by: NachoSoto <ignaciosoto90@gmail.com>
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.

2 participants