Skip to content

Commit ac2caa2

Browse files
committed
fixup: try/catch and freeze context
Signed-off-by: Todd Baert <[email protected]>
1 parent 1dc6ced commit ac2caa2

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

packages/server/src/client/internal/open-feature-client.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,19 @@ export class OpenFeatureClient implements Client {
225225
}
226226

227227
track(occurrenceKey: string, context: EvaluationContext, occurrenceDetails: TrackingEventDetails): void {
228+
try {
229+
this.shortCircuitIfNotReady();
228230

229-
this.shortCircuitIfNotReady();
230-
231-
const mergedContext = this.mergeContexts(context);
231+
const mergedContext = this.mergeContexts(context);
232+
Object.freeze(mergedContext);
232233

233-
if (typeof this._provider.track === 'function') {
234-
return this._provider.track?.(occurrenceKey, mergedContext, occurrenceDetails);
235-
} else {
236-
this._logger.debug('Provider does not implement track function: will no-op.');
234+
if (typeof this._provider.track === 'function') {
235+
return this._provider.track?.(occurrenceKey, mergedContext, occurrenceDetails);
236+
} else {
237+
this._logger.debug('Provider does not support the track function; will no-op.');
238+
}
239+
} catch (err) {
240+
this._logger.debug('Error recording tracking event.', err);
237241
}
238242
}
239243

packages/server/test/client.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,11 +837,25 @@ describe('OpenFeatureClient', () => {
837837
const invocationContextValue = 'invocationValue';
838838

839839
it('should no-op and not throw if tracking not defined on provider', async () => {
840-
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER, initialize: undefined });
840+
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER, track: undefined });
841841
const client = OpenFeature.getClient();
842842

843843
expect(() => {
844-
client.track(eventName, trackingDetails);
844+
client.track(eventName, {}, trackingDetails);
845+
}).not.toThrow();
846+
});
847+
848+
it('should no-op and not throw if provider throws', async () => {
849+
await OpenFeature.setProviderAndWait({
850+
...MOCK_PROVIDER,
851+
track: () => {
852+
throw new Error('fake error');
853+
},
854+
});
855+
const client = OpenFeature.getClient();
856+
857+
expect(() => {
858+
client.track(eventName, {}, trackingDetails);
845859
}).not.toThrow();
846860
});
847861

packages/web/src/client/internal/open-feature-client.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,22 @@ export class OpenFeatureClient implements Client {
183183
}
184184

185185
track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails): void {
186+
try {
187+
this.shortCircuitIfNotReady();
186188

187-
this.shortCircuitIfNotReady();
188-
189-
const context = {
190-
...OpenFeature.getContext(this?.options?.domain),
191-
};
189+
// copy and freeze the context
190+
const context = {
191+
...OpenFeature.getContext(this?.options?.domain),
192+
};
193+
Object.freeze(context);
192194

193-
if (typeof this._provider.track === 'function') {
194-
return this._provider.track?.(occurrenceKey, context, occurrenceDetails);
195-
} else {
196-
this._logger.debug('Provider does not implement track function: will no-op.');
195+
if (typeof this._provider.track === 'function') {
196+
return this._provider.track?.(occurrenceKey, context, occurrenceDetails);
197+
} else {
198+
this._logger.debug('Provider does not support the track function; will no-op.');
199+
}
200+
} catch (err) {
201+
this._logger.debug('Error recording tracking event.', err);
197202
}
198203
}
199204

packages/web/test/client.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,21 @@ describe('OpenFeatureClient', () => {
647647
const contextValue = 'val';
648648

649649
it('should no-op and not throw if tracking not defined on provider', async () => {
650-
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER, initialize: undefined });
650+
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER, track: undefined });
651+
const client = OpenFeature.getClient();
652+
653+
expect(() => {
654+
client.track(eventName, trackingDetails);
655+
}).not.toThrow();
656+
});
657+
658+
it('should no-op and not throw if provider throws', async () => {
659+
await OpenFeature.setProviderAndWait({
660+
...MOCK_PROVIDER,
661+
track: () => {
662+
throw new Error('fake error');
663+
},
664+
});
651665
const client = OpenFeature.getClient();
652666

653667
expect(() => {

0 commit comments

Comments
 (0)