inherit errors from Error

This commit is contained in:
Andrey Sidorov 2012-07-17 14:47:17 +10:00
parent 8e1f501701
commit eeba3b1796
2 changed files with 44 additions and 11 deletions

View file

@ -19,10 +19,11 @@ var xerrors = require('./xerrors');
var coreRequests = require('./corereqs');
var stdatoms = require('./stdatoms');
function XClient(stream, displayNum, screenNum)
function XClient(stream, displayNum, screenNum, options)
{
EventEmitter.call(this);
this.stream = stream;
this.options = options ? options : {};
// TODO: this is probably not used
this.core_requests = {};
@ -123,17 +124,18 @@ XClient.prototype.importRequestsFromTemplates = function(target, reqs)
client.seq_num = 0;
else
client.seq_num++;
// disable long stack trace for the moment, it's too expensive
// performance when enabled (travis-ci worker, Xfvb): 70000 requests finished in 52196 ms, 1341.0989347842747 req/sec
// without: 70000 requests finished in 14904 ms, 4696.725711218465 req/sec
// MBPro, XQuartz: with 3600 req/sec, without 24200 req/sec
/*
var err = new Error;
err.name = reqName;
Error.captureStackTrace(err, arguments.callee);
client.seq2stack[client.seq_num] = err.stack;
*/
if (this.options.debug === true) {
var err = new Error();
err.name = reqName; //???
Error.captureStackTrace(err, arguments.callee);
err.timestamp = Date.now();
client.seq2stack[client.seq_num] = err;
}
// is it fast?
var args = Array.prototype.slice.call(req_proxy.arguments);
@ -318,11 +320,12 @@ XClient.prototype.expectReplyHeader = function()
if (type == 0)
{
var error_code = res[1];
var error = {};
var error = new Error();
error.error = error_code;
error.seq = seq_num;
error.message = xerrors.errorText[error_code];
error.stack = client.seq2stack[error.seq]
if (client.options.debug)
error.stack = client.seq2stack[error.seq]
// unpack error packet (32 bytes for all error types, 8 of them in CCSL header)
client.pack_stream.get(24, function(buf) {
@ -467,7 +470,7 @@ module.exports.createClient = function(initCb, display, options)
{
stream = net.createConnection(6000 + parseInt(displayNum), host);
}
var client = new XClient(stream, displayNum, screenNum);
var client = new XClient(stream, displayNum, screenNum, options);
if (initCb)
{
client.on('connect', function(display) {

30
test/errors.js Normal file
View file

@ -0,0 +1,30 @@
var x11 = require('../lib/x11');
var should = require('should');
var assert = require('assert');
describe('Client', function() {
var display;
beforeEach(function(done) {
var client = x11.createClient(function(dpy) {
display=dpy;
done();
});
});
it('should emit error which is instance of Error with seqence number corresponding to source request', function(done) {
display.client.options.debug = true;
display.client.CreateWindow(); // should emit error
var seq = display.client.seq_num;
display.client.once('error', function(err) {
assert.equal(err.constructor, Error);
assert.equal(seq, err.seq);
display.client.CreateWindow(); // should emit error
seq = display.client.seq_num;
display.client.once('error', function(err) {
assert.equal(seq, err.seq);
done();
});
});
});
});