Error 'Could not get number '
With the following code:
var phone_number = "12345"; var phone = client.getPhoneNumber(phone_number); phone.setup(function() { })
If phone_number does not exist in your account, the following is thrown:
throw new Error('Could not get number ' +
^
Error: Could not get number 1234
at /Users/robert/colingo/Express/node_modules/twilio/lib/twilio.js:142:27
at IncomingMessage.<anonymous> (/Users/robert/colingo/Express/node_modules/twilio/lib/rest-client.js:86:41)
at IncomingMessage.emit (events.js:81:20)
at HTTPParser.onMessageComplete (http.js:133:23)
at CleartextStream.ondata (http.js:1231:22)
at CleartextStream._push (tls.js:303:27)
at SecurePair.cycle (tls.js:577:20)
at EncryptedStream.write (tls.js:96:13)
at Socket.ondata (stream.js:36:26)
at Socket.emit (events.js:64:17)
I have found that putting try/catch around the call to phone.setup() does not actually catch the error.
Two questions:
1.) Why does my try/catch not work?
2.) Is there a better way to check that a number exists in an account?
Ah, yes. This is because the error is happening asynchronously. That is, setup() goes and makes an asynchronous request to Twilio, having stored a reference to your callback. When the error is thrown, your call to setup() has already returned, and hence falls outside of the scope of your try-catch block.
The right thing to do here would be to clean up the interface such that instead of throw'ing the error, the error is passed into the callback for setup. This would make the call work like...
phone.setup(function(err) { if (err) { // Here, error would be the error that's being thrown. } });
Unfortunately, I didn't write the library that way. Also, I have so little spare time right now that finishing the refactor job I've started is going to take a while. I do accept pull requests though!
Happy hacking, Stephen
On Mon, Jan 30, 2012 at 1:58 PM, robertjd < [email protected]
wrote:
With the following code:
var phone_number = "12345"; var phone = client.getPhoneNumber(phone_number); phone.setup(function() { })
If phone_number does not exist in your account, the following is thrown:
throw new Error('Could not get number ' + ^ Error: Could not get number 1234 at /Users/robert/colingo/Express/node_modules/twilio/lib/twilio.js:142:27 at IncomingMessage.<anonymous> (/Users/robert/colingo/Express/node_modules/twilio/lib/rest-client.js:86:41) at IncomingMessage.emit (events.js:81:20) at HTTPParser.onMessageComplete (http.js:133:23) at CleartextStream.ondata (http.js:1231:22) at CleartextStream._push (tls.js:303:27) at SecurePair.cycle (tls.js:577:20) at EncryptedStream.write (tls.js:96:13) at Socket.ondata (stream.js:36:26) at Socket.emit (events.js:64:17)I have found that putting try/catch around the call to phone.setup() does not actually catch the error.
Two questions:
1.) Why does my try/catch not work?
2.) Is there a better way to check that a number exists in an account?
Reply to this email directly or view it on GitHub: https://github.com/sjwalter/node-twilio/issues/22
Hey Stephen, thanks for the info on the try/catch issue! I has a suspicion it was such.
I will need to do the modifications you mention (passing back an error parameter) to make this robust for our app. I'll definitely submit a pull when I've got that done.
Cheers
This library is deprecated in favor of the official library - https://github.com/twilio/twilio-node