Sms integration fix (#130)
* Twilio integration error handling improvement * Mock-sms error triggering added
This commit is contained in:
parent
9d6f3e6d9b
commit
dd7e447e73
3 changed files with 31 additions and 19 deletions
|
|
@ -1,8 +1,14 @@
|
|||
const _ = require('lodash/fp')
|
||||
|
||||
exports.NAME = 'MockSMS'
|
||||
|
||||
exports.sendMessage = function sendMessage (account, rec) {
|
||||
console.log('Sending SMS: %j', rec)
|
||||
return new Promise(resolve => {
|
||||
setTimeout(resolve, 10)
|
||||
return new Promise((resolve, reject) => {
|
||||
if (_.endsWith('666', _.getOr(false, 'sms.toNumber', rec))) {
|
||||
reject(new Error(`${exports.NAME} mocked error!`))
|
||||
} else {
|
||||
setTimeout(resolve, 10)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}`)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue