Skip to content

Instantly share code, notes, and snippets.

@bfuster
Created November 15, 2013 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfuster/7491851 to your computer and use it in GitHub Desktop.
Save bfuster/7491851 to your computer and use it in GitHub Desktop.
Pubnub try/catch JS
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