Created
November 15, 2013 21:22
-
-
Save bfuster/7491851 to your computer and use it in GitHub Desktop.
Pubnub try/catch JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Pubnub = require('pubnub'); | |
var Async = require('async'); | |
var Assert = require('assert'); | |
//for testing purposes | |
process.on('uncaughtException', function (err) { | |
console.log('Caught exception: ' + err); | |
}); | |
var pubnub = Pubnub.init({ | |
publish_key: "KEY", | |
subscribe_key: "KEY" | |
}); | |
var receivedCounters = {}; | |
var withoutTryCatchChannel = "testing_channel_without_trycatch"; | |
var withTryCatchChannel = "testing_channel_with_trycatch"; | |
receivedCounters[withoutTryCatchChannel] = 0; | |
receivedCounters[withTryCatchChannel] = 0; | |
pubnub.subscribe({ | |
channel: withoutTryCatchChannel, | |
message: withoutTryCatch, | |
connect: publishMessages(withoutTryCatchChannel) | |
}); | |
pubnub.subscribe({ | |
channel: withTryCatchChannel, | |
message: withTryCatch, | |
connect: publishMessages(withTryCatchChannel) | |
}); | |
function publishMessages(channel) { | |
console.log("Subcribed to channel %s", channel); | |
return function() { | |
Async.timesSeries(2, function(n, next) { | |
pubnub.publish({ | |
channel: channel, | |
message: 'anything' | |
}, function() { | |
next(); | |
}); | |
}, function() { | |
setTimeout(function() { | |
Assert.equal(receivedCounters[channel], 2); | |
}, 2000); | |
}); | |
} | |
} | |
//When there are no try/catch here, the rest of the messages fail regardless of the channel | |
//If you get this try/catch back, everything will work as expected | |
function withoutTryCatch(message) { | |
//try { | |
console.log("received message without try catch"); | |
receivedCounters[withoutTryCatchChannel]++; | |
var test = undefined; | |
test.name.something = ""; //will throw cannot find name on undefined | |
//} catch (e) { | |
//} | |
} | |
function withTryCatch(message) { | |
console.log("received message with try catch"); | |
receivedCounters[withTryCatchChannel]++; | |
try { | |
var test = undefined; | |
test.name.something = ""; | |
} catch (e) { | |
console.error("something went wrong"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment