Skip to content

Commit

Permalink
feat: response with whole error object instead of only stack, message (
Browse files Browse the repository at this point in the history
…#38)

* feat: response with whole error object instead of only stack, message
  • Loading branch information
killagu authored and gxcsoccer committed Mar 26, 2018
1 parent 5f8c23d commit dce3fee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
22 changes: 9 additions & 13 deletions lib/follower.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Follower extends Base {
// avoid warning message
this.setMaxListeners(100);
}

get isLeader() {
return false;
}
Expand Down Expand Up @@ -69,21 +68,19 @@ class Follower extends Base {
const packet = Packet.decode(buf);
const connObj = packet.connObj;
if (connObj && connObj.type === 'invoke_result') {
let data;
if (packet.data) {
data = this.options.transcode.decode(packet.data);
}
if (connObj.success) {
let data;
if (packet.data) {
data = this.options.transcode.decode(packet.data);
}
return {
id: packet.id,
isResponse: packet.isResponse,
data,
};
}
const error = new Error(connObj.message);
if (connObj.stack) {
error.stack = connObj.stack;
}
const error = new Error(data.message);
Object.assign(error, data);
return {
id: packet.id,
isResponse: packet.isResponse,
Expand Down Expand Up @@ -195,7 +192,6 @@ class Follower extends Base {
data,
timeout: this.options.responseTimeout,
});

// send invoke request
this.send({
id: req.id,
Expand Down Expand Up @@ -230,9 +226,9 @@ class Follower extends Base {
debug('[Follower:%s] register to channel: %s success', this.options.name, this.options.name);
this.ready(true);
} else {
const err = new Error(res.message);
err.stack = res.stack;
this.ready(err);
const error = new Error(res.error.message);
Object.assign(error, res.error);
this.ready(error);
}
});
}
Expand Down
16 changes: 12 additions & 4 deletions lib/leader.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,20 @@ class Leader extends Base {
}

if (err) {
const data = Object.assign({
stack: err.stack,
name: err.name,
message: err.message,
}, err);
err.method = connObj.method;
err.args = connObj.args;
this._errorHandler(err);

res.connObj = {
type: 'invoke_result',
success: false,
message: err.message,
stack: err.stack,
};
res.data = this._transcode.encode(data);
} else {
debug('[Leader:%s] send method:%s result to follower, result: %j', this.options.name, connObj.method, result);
const data = this._transcode.encode(result);
Expand All @@ -363,10 +367,14 @@ class Leader extends Base {
this.ready(err => {
if (err) {
res.connObj = { type: 'register_channel_res' };
res.data = this._transcode.encode({
success: false,
const data = Object.assign({
message: err.message,
stack: err.stack,
name: err.name,
}, err);
res.data = this._transcode.encode({
success: false,
error: data,
});
} else {
res.connObj = { type: 'register_channel_res' };
Expand Down
6 changes: 5 additions & 1 deletion test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ describe('test/client.test.js', () => {

async _init() {
await sleep(1000);
throw new Error('mock error');
const error = new Error('mock error');
error.code = 'ERROR_CODE';
throw error;
}

send(data) {
Expand Down Expand Up @@ -232,6 +234,7 @@ describe('test/client.test.js', () => {
assert(false);
} catch (err) {
assert(err && err.message === 'mock error');
assert.strictEqual(err.code, 'ERROR_CODE');
}

const follower = new APIClient();
Expand All @@ -241,6 +244,7 @@ describe('test/client.test.js', () => {
assert(false);
} catch (err) {
assert(err && err.message === 'mock error');
assert.strictEqual(err.code, 'ERROR_CODE');
}

await follower.close();
Expand Down

0 comments on commit dce3fee

Please sign in to comment.