From 6aa965af58d7be5059e44979884b786356a257a0 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Mon, 8 Aug 2022 22:17:42 -0700 Subject: [PATCH] Make tracking and email sending not throw on failure --- functions/src/analytics.ts | 16 +++++++++------- functions/src/send-email.ts | 17 +++++++++++------ functions/src/utils.ts | 9 +++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/functions/src/analytics.ts b/functions/src/analytics.ts index d178ee0f..e0199616 100644 --- a/functions/src/analytics.ts +++ b/functions/src/analytics.ts @@ -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 ) => { - 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, + }) + ) } diff --git a/functions/src/send-email.ts b/functions/src/send-email.ts index 5f11ed47..11d096fd 100644 --- a/functions/src/send-email.ts +++ b/functions/src/send-email.ts @@ -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 } diff --git a/functions/src/utils.ts b/functions/src/utils.ts index 0414b01e..721f33d0 100644 --- a/functions/src/utils.ts +++ b/functions/src/utils.ts @@ -42,6 +42,15 @@ export const writeAsync = async ( } } +export const tryOrLogError = async (task: Promise) => { + try { + return await task + } catch (e) { + console.error(e) + return null + } +} + export const isProd = () => { return admin.instanceId().app.options.projectId === 'mantic-markets' }