diff --git a/lib/MailgunSend.js b/lib/MailgunSend.js index 5763ac9..7ff330a 100644 --- a/lib/MailgunSend.js +++ b/lib/MailgunSend.js @@ -65,14 +65,9 @@ class MailgunSend { * @param {object} creds Object of options for sending an email * @return {Promise} Promise that will complete when email sends */ - send(options) { - return new Promise((fulfill, reject) => { - try { - MailgunSend.validate(options); - } catch (e) { - reject(e); - return false; - } + async send(options) { + try { + MailgunSend.validate(options); const mailOpts = { from: options.from, @@ -100,33 +95,31 @@ class MailgunSend { mailOpts.bcc = options.bcc; } - return this.mailgun.messages.create(this.domain, mailOpts) - .then((response) => { - const { id, message } = response; - let successMessage = `\n\t🚀 ✉️ Email was successfully sent to ${options.to}!\n`; - - if (options.verbose) { - successMessage += `\n\t🗒 API Message: "${message}"\n\n\t🆔 Message ID: ${id}\n`; - } - - fulfill(successMessage); - }) - .catch((err) => { - if (err.status === 401) { - reject(new Error( - '\n\t🚫 Your Mailgun API key and/or domain are incorrect.' + const response = await this.mailgun.messages.create(this.domain, mailOpts); + + const { id, message } = response; + let successMessage = `\n\t🚀 ✉️ Email was successfully sent to ${options.to}!\n`; + + if (options.verbose) { + successMessage += `\n\t🗒 API Message: "${message}"\n\n\t🆔 Message ID: ${id}\n`; + } + + return successMessage; + } catch (err) { + if (err.status === 401) { + throw new Error( + '\n\t🚫 Your Mailgun API key and/or domain are incorrect.' + '\n\t🔁 Rerun your command with -R flag to re-enter you credentials.', - )); - } else if (err.type === 'EUNAVAILABLE') { - reject(new Error( - '\n\t‼️ 🌐 Looks like you may not be connected to the internet. ' + ); + } else if (err.type === 'EUNAVAILABLE') { + throw new Error( + '\n\t‼️ 🌐 Looks like you may not be connected to the internet. ' + 'Check your network settings and try again.', - )); - } else { - reject(new Error(`\n\t🚫 ${err.message}`)); - } - }); - }); + ); + } else { + throw err; + } + } } }