Make tracking and email sending not throw on failure
This commit is contained in:
parent
a1861f1fda
commit
6aa965af58
|
@ -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,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user