Skip to content

Commit

Permalink
[3.1.1] Log invalid data when JSON parse fails (#136)
Browse files Browse the repository at this point in the history
Log invalid data when JSON parse fails
  • Loading branch information
markmur authored Nov 1, 2024
1 parent cbafef7 commit f0cc289
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.1.1 - November 1, 2024

- Logs invalid JSON for lifecycle events

## 3.1.0 - October 25, 2024

Upgrades Swift dependency to
Expand Down
2 changes: 1 addition & 1 deletion modules/@shopify/checkout-sheet-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shopify/checkout-sheet-kit",
"license": "MIT",
"version": "3.1.0",
"version": "3.1.1",
"main": "lib/commonjs/index.js",
"types": "src/index.ts",
"source": "src/index.ts",
Expand Down
11 changes: 7 additions & 4 deletions modules/@shopify/checkout-sheet-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,17 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
switch (event) {
case 'pixel':
eventCallback = this.interceptEventEmission(
'pixel',
callback,
this.parseCustomPixelData,
);
break;
case 'completed':
eventCallback = this.interceptEventEmission(callback);
eventCallback = this.interceptEventEmission('completed', callback);
break;
case 'error':
eventCallback = this.interceptEventEmission(
'error',
callback,
this.parseCheckoutError,
);
Expand Down Expand Up @@ -169,6 +171,7 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
* Event data can be sent back as either a parsed Event object or a JSON string.
*/
private interceptEventEmission(
event: CheckoutEvent,
callback: CheckoutEventCallback,
transformData?: (data: any) => any,
): (eventData: string | typeof callback) => void {
Expand All @@ -181,20 +184,20 @@ class ShopifyCheckoutSheet implements ShopifyCheckoutSheetKit {
callback(parsed);
} catch (error) {
const parseError = new LifecycleEventParseError(
'Failed to parse event data: Invalid JSON',
`Failed to parse "${event}" event data: Invalid JSON`,
{
cause: 'Invalid JSON',
},
);
// eslint-disable-next-line no-console
console.error(parseError);
console.error(parseError, eventData);
}
} else if (eventData && typeof eventData === 'object') {
callback(transformData?.(eventData) ?? eventData);
}
} catch (error) {
const parseError = new LifecycleEventParseError(
'Failed to parse event data',
`Failed to parse "${event}" event data`,
{
cause: 'Unknown',
},
Expand Down
16 changes: 12 additions & 4 deletions modules/@shopify/checkout-sheet-kit/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,12 @@ describe('ShopifyCheckoutSheetKit', () => {
'pixel',
expect.any(Function),
);
eventEmitter.emit('pixel', '{"someAttribute": 123');
expect(mock).toHaveBeenCalledWith(expect.any(LifecycleEventParseError));
const invalidData = '{"someAttribute": 123';
eventEmitter.emit('pixel', invalidData);
expect(mock).toHaveBeenCalledWith(
expect.any(LifecycleEventParseError),
invalidData,
);
});
});

Expand Down Expand Up @@ -322,8 +326,12 @@ describe('ShopifyCheckoutSheetKit', () => {
'completed',
expect.any(Function),
);
eventEmitter.emit('completed', 'INVALID JSON');
expect(mock).toHaveBeenCalledWith(expect.any(LifecycleEventParseError));
const invalidData = 'INVALID JSON';
eventEmitter.emit('completed', invalidData);
expect(mock).toHaveBeenCalledWith(
expect.any(LifecycleEventParseError),
invalidData,
);
});
});

Expand Down
6 changes: 5 additions & 1 deletion sample/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,7 @@ PODS:
- Yoga
- ShopifyCheckoutSheetKit (3.1.1)
- SocketRocket (0.7.0)
- SwiftLint (0.57.0)
- Yoga (0.0.0)

DEPENDENCIES:
Expand Down Expand Up @@ -1327,12 +1328,14 @@ DEPENDENCIES:
- RNScreens (from `../node_modules/react-native-screens`)
- "RNShopifyCheckoutSheetKit (from `../../modules/@shopify/checkout-sheet-kit`)"
- RNVectorIcons (from `../node_modules/react-native-vector-icons`)
- SwiftLint
- Yoga (from `../../node_modules/react-native/ReactCommon/yoga`)

SPEC REPOS:
trunk:
- ShopifyCheckoutSheetKit
- SocketRocket
- SwiftLint

EXTERNAL SOURCES:
boost:
Expand Down Expand Up @@ -1525,8 +1528,9 @@ SPEC CHECKSUMS:
RNVectorIcons: 50ea777efffdd991a22e968aa312d75da7ff46c3
ShopifyCheckoutSheetKit: fe309799b18b8d554f28c3f075d6d57d4811c9ab
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
SwiftLint: eb47480d47c982481592c195c221d11013a679cc
Yoga: 348f8b538c3ed4423eb58a8e5730feec50bce372

PODFILE CHECKSUM: 4a9ceecdcfb54884b51163a7e4438cd49c73c8d9
PODFILE CHECKSUM: 9efd19a381198fb46f36acf3d269233039fb9dc5

COCOAPODS: 1.15.2

0 comments on commit f0cc289

Please sign in to comment.