[Statsig] Protect against early logEvent call crashing (#3315)

* [Statsig] Check if initialized

* Never interrupt the caller
zio/stable
dan 2024-03-21 17:01:55 +00:00 committed by GitHub
parent 7503d83eaa
commit 55fb81867b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 5 deletions

View File

@ -46,11 +46,21 @@ export function logEvent<E extends keyof LogEvents>(
eventName: E & string, eventName: E & string,
rawMetadata: LogEvents[E] & FlatJSONRecord, rawMetadata: LogEvents[E] & FlatJSONRecord,
) { ) {
const fullMetadata = { try {
...rawMetadata, const fullMetadata = {
} as Record<string, string> // Statsig typings are unnecessarily strict here. ...rawMetadata,
fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)' } as Record<string, string> // Statsig typings are unnecessarily strict here.
Statsig.logEvent(eventName, null, fullMetadata) fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)'
if (Statsig.initializeCalled()) {
Statsig.logEvent(eventName, null, fullMetadata)
}
} catch (e) {
// A log should never interrupt the calling code, whatever happens.
// Rethrow on a clean stack.
setTimeout(() => {
throw e
})
}
} }
export function useGate(gateName: string) { export function useGate(gateName: string) {