Skip to content

Instantly share code, notes, and snippets.

@chilts
Created February 28, 2014 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilts/9263441 to your computer and use it in GitHub Desktop.
Save chilts/9263441 to your computer and use it in GitHub Desktop.
diff --git a/mailer.js b/mailer.js
index d5a46c6..5518639 100644
--- a/mailer.js
+++ b/mailer.js
@@ -67,6 +67,13 @@ module.exports = function (config, log) {
var p = P.defer()
var remaining = config.i18n.supportedLanguages.length * types.length;
+
+ function checkRemaining() {
+ if ( remaining === 0 ) {
+ p.resolve()
+ }
+ }
+
config.i18n.supportedLanguages.forEach(function(lang) {
// somewhere to store the templates
templates[lang] = templates[lang] || {}
@@ -79,16 +86,32 @@ module.exports = function (config, log) {
request(opts, function(err, res, body) {
if (err) return log.warn({ op: 'mailer.fetchTemplates', err: err })
- templates[lang][type] = body
+ remaining -= 1
- // compile the templates
- templates[lang][type].html = handlebars.compile(templates[lang][type].html)
- templates[lang][type].text = handlebars.compile(templates[lang][type].text)
+ // only compile the templates and save this one if subject, html and text are all defined
+ if ( !body.subject || !body.html || !body.text ) {
+ log.warn({ op: 'mailer.fetchTemplates', err: 'subject/text/html not all given for this language ' + lang })
+ checkRemaining()
+ return;
+ }
- remaining -= 1
- if ( remaining === 0 ) {
- p.resolve()
+ // compile the templates
+ try {
+ var html = handlebars.compile(body.html)
+ var text = handlebars.compile(body.text)
}
+ catch (err) {
+ log.warn({ op: 'mailer.fetchTemplates', err: err })
+ checkRemaining()
+ return;
+ }
+
+ // all good, so save each field
+ templates[lang][type] = body
+ templates[lang][type].html = html
+ templates[lang][type].text = text
+
+ checkRemaining()
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment