Skip to content

Commit

Permalink
Try-catch for both logAmplitudeEvent and logCustomAmplitudeEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
haakomol committed Jan 20, 2025
1 parent 3cb19bf commit 9c81d23
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/libs/amplitude/amplitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,7 @@ export async function logAmplitudeEvent(
): Promise<void> {
const { eventType, eventProperties } = taxonomyToAmplitudeEvent(event, extraData)

if (isLocalOrDemo) {
console.log('Amplitude event: ' + eventType)
if (eventProperties) {
console.log(eventProperties)
}

return
}

try {
// This can throw an error (rejected promise), therefore try-catch
await dekoratorenAmplitudeLogger(eventType, {
...eventProperties,
})
} catch (error) {
pinoLogger.error(`Could not log event to Amplitude. Message: ${(error as Error)?.message}`)
}
await logAmplitudeEventUsingDekoratorenInstance(eventType, eventProperties)
}

export async function logCustomAmplitudeEvent(event: string, extraData?: Record<string, unknown>): Promise<void> {
Expand All @@ -76,10 +60,22 @@ export async function logCustomAmplitudeEvent(event: string, extraData?: Record<
...extraData,
}

await logAmplitudeEventUsingDekoratorenInstance(event, eventProperties)
}

async function logAmplitudeEventUsingDekoratorenInstance(
event: string,
eventProperties: Record<string, unknown>,
): Promise<void> {
if (isLocalOrDemo) {
console.log(`Custom Amplitude event: ${event}`, eventProperties)
console.log(`Amplitude event: ${event}, eventProperties:\n${(JSON.stringify(eventProperties ?? {}), null, 2)}`)
return
}

await dekoratorenAmplitudeLogger(event, eventProperties)
try {
// This can throw an error (rejected promise), therefore try-catch
await dekoratorenAmplitudeLogger(event, eventProperties)
} catch (error) {
pinoLogger.error(`Could not log event to Amplitude. Message: ${(error as Error)?.message}`)
}
}

0 comments on commit 9c81d23

Please sign in to comment.