[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,12 +46,22 @@ export function logEvent<E extends keyof LogEvents>(
eventName: E & string,
rawMetadata: LogEvents[E] & FlatJSONRecord,
) {
try {
const fullMetadata = {
...rawMetadata,
} as Record<string, string> // Statsig typings are unnecessarily strict here.
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) {
const {isLoading, value} = useStatsigGate(gateName)