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

View File

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