Skip to content

Commit

Permalink
fix: biz error should be with resultCode:01 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored Jun 29, 2018
1 parent 5c3f7ce commit c7ca73a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/client/consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class RpcConsumer extends Base {
return this.options.registry;
}

get targetAppName() {
return this.options.targetAppName;
}

get registryConfig() {
return {
protocol: 'bolt',
Expand Down Expand Up @@ -84,6 +88,7 @@ class RpcConsumer extends Base {

createRequest(method, args, options) {
return new RpcRequest({
targetAppName: this.targetAppName,
serverSignature: this.id,
methodName: method,
args,
Expand Down Expand Up @@ -118,8 +123,10 @@ class RpcConsumer extends Base {
}
return res.appResponse;
} catch (err) {
if (req.meta.resultCode === '00') {
req.meta.resultCode = '01';
}
if (this.options.errorAsNull !== true) throw err;

this.logger.warn(err);
return null;
} finally {
Expand Down
1 change: 1 addition & 0 deletions lib/client/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class RpcRequest extends Base {
assert(data.timeout, '[RpcRequest] req.timeout is required');
super();

this.targetAppName = data.targetAppName;
this.serverSignature = data.serverSignature;
this.methodName = data.methodName;
this.args = data.args;
Expand Down
5 changes: 5 additions & 0 deletions test/client/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe('test/client/client.test.js', () => {
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
targetAppName: 'pb',
});
let req;
consumer.once('request', r => { req = r; });
assert(consumer.targetAppName === 'pb');
assert(client.consumerMap && client.consumerMap.size === 1);
assert(client.consumerMap.get('com.alipay.sofa.rpc.test.ProtoService:1.0@SOFA@pb') === consumer);
await consumer.ready();
Expand All @@ -56,6 +59,8 @@ describe('test/client/client.test.js', () => {
const res = await consumer.invoke('echoObj', args);
assert.deepEqual(res, { code: 200, message: 'hello Peter, you are in A' });

assert(req && req.targetAppName === 'pb');

await client.close();
});
});
55 changes: 55 additions & 0 deletions test/client/consumer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,59 @@ describe('test/client/consumer.test.js', () => {
assert(!res.appResponse);
consumer.close();
});


it('should throw BizError', async function() {
const consumer = new RpcConsumer({
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
connectionManager,
connectionOpts: {
protocol,
},
registry,
logger,
});
await consumer.ready();
let req;
let res;
consumer.once('request', val => {
req = val;
});
consumer.once('response', val => {
assert(val && val.req && val.res);
res = val.res;
});

const args = [{
name: 'XIAOCHEN',
group: 'A',
}];
try {
await consumer.invoke('echoObj', args);
assert(false);
} catch (err) {
assert(err.message.includes('mock error'));
}

assert(req);
assert(req.serverSignature === 'com.alipay.sofa.rpc.test.ProtoService:1.0');
assert(req.methodName === 'echoObj');
assert.deepEqual(req.args, args);
assert(req.timeout === 3000);
assert(req.meta.id);
assert(req.meta.resultCode === '01');
assert(req.meta.connectionGroup === 'com.alipay.sofa.rpc.test.ProtoService:1.0@SOFA');
assert(req.meta.codecType === 'protobuf');
assert(req.meta.boltVersion === 1);
assert(!req.meta.crcEnable);
assert(req.meta.timeout === 3000);
assert(req.meta.reqSize === 271);
assert(req.meta.resSize === 113);
assert(req.meta.rt >= 0);

assert(res && res.error && res.error.message.includes('mock error'));
console.log(res.error);
assert(!res.appResponse);
consumer.close();
});
});
2 changes: 2 additions & 0 deletions test/supports/pb_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ exports.start = async function() {
interfaceName: 'com.alipay.sofa.rpc.test.ProtoService',
}, {
async echoObj(req) {
if (req.name === 'XIAOCHEN') throw new Error('mock error');

req = req.toObject({ enums: String });
await sleep(10);
return {
Expand Down

0 comments on commit c7ca73a

Please sign in to comment.