Skip to content

Commit

Permalink
refactor: better error logging for our contact us submission API (#314)
Browse files Browse the repository at this point in the history
* refactor: more stability and better logging of info, warn and error

* fix: always redirect user to thank you page, but log the error
  • Loading branch information
daine authored Apr 10, 2024
1 parent 8cb59d6 commit 2ddbc96
Showing 1 changed file with 47 additions and 17 deletions.
64 changes: 47 additions & 17 deletions api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,63 @@ process.on('SIGTERM', async () => {
process.exit(0)
});

const redirectUser = (origin, forwardedOrigin, lang, res) => {
// Attempt to get origin URL from request. If origin is null, use the default domains (en or fr) based on language
origin = origin && origin !== 'null' ? origin : forwardedOrigin
origin = origin && origin !== 'null' ? origin : lang === 'en' ? DOMAIN_EN : DOMAIN_FR

const contactPath = lang == 'en' ? '/en/contact/thanks' : '/fr/contactez/merci'
const redirectTo = origin + contactPath
console.log(`[INFO] Redirecting to ${redirectTo}`)
res.redirect(303, redirectTo)
}

app.post('/submission', async (req, res) => {
let origin = req.get('origin');
const body = req.body

const body = (req.body) ? req.body : {}
const forwardedHost = req.get('x-forwarded-host')
const forwardedProto = req.get('x-forwarded-proto')

const forwardedOrigin = forwardedHost && forwardedProto ? `${forwardedProto}://${forwardedHost}` : null

// Get the language to figure out which domain to redirect the user to
// Form name is in the format "contactEN" or "contactFR"
const lang = body["form-name"].slice(-2).toLowerCase()
let lang = "en"
try {
lang = body["form-name"].slice(-2).toLowerCase()
} catch (e) {
console.warn(`[WARN] Unable to determine language from form name, using default ${lang}`)
}

const parameters = await getParametersByName({
'gc-design-system-config': { transform: 'json' }
}, { decrypt: true });
let parameters
if( process.env['NODE_ENV'] === 'development' ) {
parameters = {
'gc-design-system-config': {
EMAIL_TARGET: '',
NOTIFY_API_KEY: '',
NOTIFY_TEMPLATE_ID: ''
}
}
} else {
try {
parameters = await getParametersByName({
'gc-design-system-config': {transform: 'json'}
}, {decrypt: true});
} catch (e) {
// Log the error, but return the user back to the site (thank you page)
console.error('[ERROR] Failed to get parameters from SSM', e)
redirectUser(origin, forwardedOrigin, lang, res)
}
}

const { EMAIL_TARGET, NOTIFY_API_KEY, NOTIFY_TEMPLATE_ID } = parameters['gc-design-system-config'];

const {name, email, message, reasonForContact, honeypot} = body

// Honeypot check
if (honeypot && honeypot.length > 0) {
console.error('Honeypot detected')
console.warn('[WARN] Honeypot detected')
res.status(204).send()
return
}
Expand All @@ -67,29 +102,24 @@ app.post('/submission', async (req, res) => {
},
});

console.log('[INFO] Sending to Notify: ', postData)

await axios
.post(
'https://api.notification.canada.ca/v2/notifications/email',
postData,
{headers: headData},
)
.then(res => {
console.log('RESPONSE RECEIVED: ', res);
console.log('[INFO] Successfully sent to Notify. Status: ', res.status);
})
.catch(err => {
console.log('AXIOS ERROR: ', err);
console.error('[ERROR] Failed to send to Notify', err)
});

// Attempt to get origin URL from request. If origin is null, use the default domains
origin = origin && origin !== 'null' ? origin : forwardedOrigin
origin = origin && origin !== 'null' ? origin : lang === 'en' ? DOMAIN_EN : DOMAIN_FR

const contactPath = lang == 'en' ? '/en/contact/thanks' : '/fr/contactez/merci'
const redirectTo = origin + contactPath
console.log(`Redirecting to ${redirectTo}`)
res.redirect(303, redirectTo)
redirectUser(origin, forwardedOrigin, lang, res)
})

app.listen(port, () => {
console.log(`API listening at http://localhost:${port}`)
console.log(`[INFO] API listening at http://localhost:${port}`)
})

0 comments on commit 2ddbc96

Please sign in to comment.