Sms integration fix (#130)

* Twilio integration error handling improvement

* Mock-sms error triggering added
This commit is contained in:
Fabio Cigliano 2018-06-19 22:42:51 +12:00 committed by Josh Harvey
parent 9d6f3e6d9b
commit dd7e447e73
3 changed files with 31 additions and 19 deletions

View file

@ -7,17 +7,22 @@ const BAD_NUMBER_CODES = [21201, 21202, 21211, 21214, 21216, 21217, 21219, 21408
21610, 21612, 21614, 21608]
function sendMessage (account, rec) {
const client = twilio(account.accountSid, account.authToken)
const body = rec.sms.body
const _toNumber = rec.sms.toNumber || account.toNumber
return Promise.resolve()
.then(() => {
// to catch configuration errors like
// "Error: username is required"
const client = twilio(account.accountSid, account.authToken)
const body = rec.sms.body
const _toNumber = rec.sms.toNumber || account.toNumber
const opts = {
body: body,
to: _toNumber,
from: account.fromNumber
}
const opts = {
body: body,
to: _toNumber,
from: account.fromNumber
}
return client.messages.create(opts)
return client.messages.create(opts)
})
.catch(err => {
if (_.includes(err.code, BAD_NUMBER_CODES)) {
const badNumberError = new Error(err.message)
@ -25,7 +30,7 @@ function sendMessage (account, rec) {
throw badNumberError
}
throw new Error(err.message)
throw new Error(`Twilio error: ${err.message}`)
})
}