Skip to content

Commit 33f6fce

Browse files
fix(analytics): make service error-resilient, fix 5 failing tests
Wrap track/identify/groupIdentify in try/catch so the analytics service never throws — callers no longer need defensive try/catch wrappers. Update resilience tests to expect not.toThrow instead of toThrow. Closes #3340
1 parent 9be1f61 commit 33f6fce

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

lib/services/analytics.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const init = async () => {
3737
*/
3838
const track = (distinctId, event, properties, groups) => {
3939
if (!client) return;
40-
client.capture({ distinctId, event, properties, groups });
40+
try {
41+
client.capture({ distinctId, event, properties, groups });
42+
} catch (_) { /* analytics must never break caller */ }
4143
};
4244

4345
/**
@@ -48,7 +50,9 @@ const track = (distinctId, event, properties, groups) => {
4850
*/
4951
const identify = (distinctId, properties) => {
5052
if (!client) return;
51-
client.identify({ distinctId, properties });
53+
try {
54+
client.identify({ distinctId, properties });
55+
} catch (_) { /* analytics must never break caller */ }
5256
};
5357

5458
/**
@@ -60,7 +64,9 @@ const identify = (distinctId, properties) => {
6064
*/
6165
const groupIdentify = (groupType, groupKey, properties) => {
6266
if (!client) return;
63-
client.groupIdentify({ groupType, groupKey, properties });
67+
try {
68+
client.groupIdentify({ groupType, groupKey, properties });
69+
} catch (_) { /* analytics must never break caller */ }
6470
};
6571

6672
/**

lib/services/tests/analytics.service.resilience.unit.tests.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ describe('Analytics service resilience tests:', () => {
4141
});
4242

4343
// ─────────────────────────────────────────────────────────────────────
44-
// Error propagation — synchronous SDK failures
44+
// Error resilience — synchronous SDK failures are swallowed
4545
// ─────────────────────────────────────────────────────────────────────
46-
describe('synchronous SDK error propagation:', () => {
47-
test('track should propagate when client.capture throws', () => {
46+
describe('synchronous SDK error resilience:', () => {
47+
test('track should not throw when client.capture throws', () => {
4848
mockPostHogInstance.capture.mockImplementation(() => {
4949
throw new Error('Capture error');
5050
});
5151

52-
expect(() => AnalyticsService.track('u1', 'evt')).toThrow('Capture error');
52+
expect(() => AnalyticsService.track('u1', 'evt')).not.toThrow();
5353
});
5454

55-
test('identify should propagate when client.identify throws', () => {
55+
test('identify should not throw when client.identify throws', () => {
5656
mockPostHogInstance.identify.mockImplementation(() => {
5757
throw new Error('Identify error');
5858
});
5959

60-
expect(() => AnalyticsService.identify('u1', {})).toThrow('Identify error');
60+
expect(() => AnalyticsService.identify('u1', {})).not.toThrow();
6161
});
6262

63-
test('groupIdentify should propagate when client.groupIdentify throws', () => {
63+
test('groupIdentify should not throw when client.groupIdentify throws', () => {
6464
mockPostHogInstance.groupIdentify.mockImplementation(() => {
6565
throw new Error('Group error');
6666
});
6767

68-
expect(() => AnalyticsService.groupIdentify('company', 'org', {})).toThrow('Group error');
68+
expect(() => AnalyticsService.groupIdentify('company', 'org', {})).not.toThrow();
6969
});
7070
});
7171

0 commit comments

Comments
 (0)