Skip to content

Instantly share code, notes, and snippets.

@networkimprov
Last active August 29, 2015 14:00
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 networkimprov/11132726 to your computer and use it in GitHub Desktop.
Save networkimprov/11132726 to your computer and use it in GitHub Desktop.
var lib = require('./buglib.js');
lib.on('connect', function() {
console.log('library ready');
throw new Error('test throw');
console.log('done');
});
lib.init(null);
var lib2 = require('./buglib2.js');
var sEvents = {};
module.exports.on = function(event, callback) {
if (typeof event !== 'string' || typeof callback !== 'function')
throw new Error('arguments are: String event, Function callback');
sEvents[event] = callback;
};
module.exports.init = function(params) {
lib2.init(params, function(op, data) {
if (sEvents[op])
return sEvents[op](data);
if (op === 'error')
throw data;
});
};
var cp = require('child_process');
var fs = require('fs');
var net = require('net');
var sSocketPath = '/tmp/bug.sock';
module.exports.init = function(params, callback) {
try {
fs.unlinkSync(sSocketPath);
} catch (err) {
if (err.code !== 'ENOENT') throw err;
}
var aSrvr = net.createServer(handleConnect);
aSrvr.listen(sSocketPath, fLoad);
function fLoad() {
var aC = cp.spawn('bash', ['-c', 'echo stuff | nc -U '+sSocketPath], {stdio:'inherit'});
aC.on('exit', function(code, signal) {
//aSrvr.close();
});
}
function handleConnect(socket) {
var aTimer = setTimeout(function() {
console.log('library connected but idle');//. disconnect
aTimer = null;
}, 2000);
socket.on('data', function(data) {
fMsg(JSON.parse('{"lib":"'+data.lib+'"}'));
});
function fMsg(jso) {
return callback('connect', jso);
}
}
};
@mtibeica
Copy link

socket.on('data', function(data) {
  try {
    fMsg(JSON.parse('{"lib":"'+data.lib+'"}'));
  } catch (e) {
    console.log(e);
  }
});

and the exception gets caught!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment