Skip to content

Instantly share code, notes, and snippets.

@eskim
Forked from cypres/client.js
Created August 21, 2012 12:58
Show Gist options
  • Save eskim/3415159 to your computer and use it in GitHub Desktop.
Save eskim/3415159 to your computer and use it in GitHub Desktop.
node.js thrift exception test
var thrift = require('thrift');
var test = require('./gen-nodejs/test.js'),
ttypes = require('./gen-nodejs/test_types');
var connection = thrift.createConnection('localhost', 9090),
client = thrift.createClient(test, connection);
connection.on('error', function(err) {
console.error(err);
});
connection.on('connect',function () {
runtest(1,function() {
runtest(2,function() {
runtest(3,function() {
connection.end();
});
});
});
});
function runtest(int, cb)
{
console.log('Test '+int);
client.foo(int,function(err,response) {
if (err) {
console.log('Exception:');
console.info(err);
cb();
} else {
console.log('Reply:');
console.info(response);
cb();
}
});
}
var thrift = require('thrift');
var test = require('./gen-nodejs/test.js'),
ttypes = require('./gen-nodejs/test_types');
var server = thrift.createServer(test, {
foo: function(bar,response) {
console.log('Got: '+bar);
if (bar == 1) {
console.log('throw MyException');
response(new ttypes.MyException({code: 0x0211, message: 'Test exception'}));
} else if (bar == 2) {
console.log('throw MyOtherException');
response(new ttypes.MyOtherException({message: 'Test exception'}));
} else {
console.log('okay');
response(null, 1234);
}
}
});
server.listen(9090);
exception MyException {
1: required string message,
2: optional list<string> variables,
3: optional i16 code
}
exception MyOtherException {
1: required string message,
2: optional list<string> variables
}
service test
{
i32 foo(1:i32 bar) throws (1:MyException me, 2:MyOtherException moe)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment