Make tracking and email sending not throw on failure

This commit is contained in:
Marshall Polaris 2022-08-08 22:17:42 -07:00
parent a1861f1fda
commit 6aa965af58
3 changed files with 29 additions and 13 deletions

View File

@ -3,7 +3,7 @@ import * as Amplitude from '@amplitude/node'
import { DEV_CONFIG } from '../../common/envs/dev'
import { PROD_CONFIG } from '../../common/envs/prod'
import { isProd } from './utils'
import { isProd, tryOrLogError } from './utils'
const key = isProd() ? PROD_CONFIG.amplitudeApiKey : DEV_CONFIG.amplitudeApiKey
@ -15,10 +15,12 @@ export const track = async (
eventProperties?: any,
amplitudeProperties?: Partial<Amplitude.Event>
) => {
await amp.logEvent({
event_type: eventName,
user_id: userId,
event_properties: eventProperties,
...amplitudeProperties,
})
return await tryOrLogError(
amp.logEvent({
event_type: eventName,
user_id: userId,
event_properties: eventProperties,
...amplitudeProperties,
})
)
}

View File

@ -1,4 +1,5 @@
import * as mailgun from 'mailgun-js'
import { tryOrLogError } from './utils'
const initMailgun = () => {
const apiKey = process.env.MAILGUN_KEY as string
@ -18,9 +19,11 @@ export const sendTextEmail = async (
// Don't rewrite urls in plaintext emails
'o:tracking-clicks': 'htmlonly',
}
const mg = initMailgun()
const result = await mg.messages().send(data)
console.log('Sent text email', to, subject)
const mg = initMailgun().messages()
const result = await tryOrLogError(mg.send(data))
if (result != null) {
console.log('Sent text email', to, subject)
}
return result
}
@ -39,8 +42,10 @@ export const sendTemplateEmail = async (
template: templateId,
'h:X-Mailgun-Variables': JSON.stringify(templateData),
}
const mg = initMailgun()
const result = await mg.messages().send(data)
console.log('Sent template email', templateId, to, subject)
const mg = initMailgun().messages()
const result = await tryOrLogError(mg.send(data))
if (result != null) {
console.log('Sent template email', templateId, to, subject)
}
return result
}

View File

@ -42,6 +42,15 @@ export const writeAsync = async (
}
}
export const tryOrLogError = async <T>(task: Promise<T>) => {
try {
return await task
} catch (e) {
console.error(e)
return null
}
}
export const isProd = () => {
return admin.instanceId().app.options.projectId === 'mantic-markets'
}