Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
],
semver: [
'mocha@3',
'byte@1',
],
};
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
Expand Down
8 changes: 8 additions & 0 deletions lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

var util = require('util');
var has = require('utility').has;

exports.DEFAULT_CLASSNAME = {
boolean: 'boolean',
Expand Down Expand Up @@ -161,4 +162,11 @@ function JavaExceptionError(obj, withType) {

util.inherits(JavaExceptionError, Error);

exports.isJavaException = function (obj) {
if (has(obj, 'detailMessage') && has(obj, 'cause') && has(obj, 'stackTrace')) {
return true;
}
return false;
};

exports.JavaExceptionError = JavaExceptionError;
3 changes: 2 additions & 1 deletion lib/v1/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var ByteBuffer = require('byte');
var is = require('is-type-of');
var utils = require('../utils');
var object = require('../object');
var isJavaException = object.isJavaException;
var JavaExceptionError = object.JavaExceptionError;

var BYTE_CODES = {};
Expand Down Expand Up @@ -410,7 +411,7 @@ proto.readObject = function (withType) {
debug('read object finish');

// java.lang.NoClassDefFoundError
if (/Exception$/.test(type) || /^java\.lang\.\w+Error$/.test(type)) {
if (isJavaException(result.$)) {
result.$ = new JavaExceptionError(result, withType);
}

Expand Down
16 changes: 13 additions & 3 deletions lib/v2/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ var is = require('is-type-of');
var debug = require('debug')('hessian:v2:decoder');
var DecoderV1 = require('../v1/decoder');
var utils = require('../utils');
var isJavaException = require('../object').isJavaException;
var JavaExceptionError = require('../object').JavaExceptionError;
var decodeFnCtx = { JavaExceptionError: JavaExceptionError };
var codegen = require('@protobufjs/codegen');

var BYTE_CODES = {};
var errorProps = {
detailMessage: true,
stackTrace: true,
cause: true,
};

function Decoder(buf, classCache) {
DecoderV1.call(this, buf);
Expand Down Expand Up @@ -474,12 +480,16 @@ proto._readObjectDefinition = function () {
// set class definition into cache
if (this.classCache) {
if (this.classCache.enableCompile) {
var errorPropCount = 0;
var gen = codegen(['decoder', 'withType'], 'decode');
gen('// %s', cachekey);
gen('var result = {');
gen(' $class: \'%s\',', classname);
gen(' $: {');
for (var field of fields) {
if (errorProps[field]) {
errorPropCount++;
}
if (INNER_CLASS_LABEL === field) {
continue;
}
Expand All @@ -495,7 +505,7 @@ proto._readObjectDefinition = function () {
gen('result.$.%s = decoder.read(withType);', field);
}
}
if (/Exception$/.test(classname)) {
if (errorPropCount === 3) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic Number 是否要常量化一下?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么是 3,没有注释说明一下?

gen('result.$ = new JavaExceptionError(result, withType);');
}
gen('return withType ? result : result.$');
Expand Down Expand Up @@ -579,7 +589,7 @@ proto.readObject = function (withType) {
result.$[name] = value;
}

if (/Exception$/.test(cls.name)) {
if (isJavaException(result.$)) {
result.$ = new JavaExceptionError(result, withType);
}

Expand Down Expand Up @@ -836,7 +846,7 @@ proto.readMap = function (withType) {
this._addRef(result);
this._readMap(result.$, withType);

if (/Exception$/.test(type)) {
if (isJavaException(result.$)) {
result.$ = new JavaExceptionError(result);
}

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
"homepage": "https://github.com/node-modules/hessian.js",
"dependencies": {
"@protobufjs/codegen": "^2.0.4",
"byte": "^1.2.0",
"byte": "^1.4.1",
"debug": "^3.1.0",
"is-type-of": "^1.2.0",
"long": "^3.2.0",
"long": "^4.0.0",
"utility": "^1.13.1"
},
"devDependencies": {
"autod": "^3.0.1",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"istanbul": "^0.4.5",
"js-to-java": "^2.4.0",
"js-to-java": "^2.5.0",
"jshint": "^2.9.5",
"mm": "^2.2.0",
"mocha": "^3.5.3",
"should": "^13.1.3"
"should": "^13.2.1"
},
"engines": {
"node": ">= 0.12.0"
Expand Down
8 changes: 8 additions & 0 deletions test/exception.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,13 @@ describe('exception.test.js', function () {
e.stack === 'java.io.IOException: 模拟测试异常; this is a java IOException instance\n at hessian.Main.main (Main.java:1303)'
);
});

it('should read exception type not endsWith Exception', function () {
var e = hessian.decode(new Buffer('4FB9636F6D2E616C697061792E736F66612E7270632E717569636B73746172742E54657374244572726F72940D64657461696C4D6573736167650563617573650A737461636B54726163651473757070726573736564457863657074696F6E736F90076D6573736167654A005674001C5B6A6176612E6C616E672E537461636B5472616365456C656D656E746E014FAB6A6176612E6C616E672E537461636B5472616365456C656D656E74940E6465636C6172696E67436C6173730A6D6574686F644E616D650866696C654E616D650A6C696E654E756D6265726F91530023636F6D2E616C697061792E736F66612E7270632E717569636B73746172742E54657374046D61696E09546573742E6A617661AE7A567400326A6176612E7574696C2E436F6C6C656374696F6E7324556E6D6F6469666961626C6552616E646F6D4163636573734C6973746E007A', 'hex'), '2.0');
assert(e instanceof Error);
assert(e.name === 'com.alipay.sofa.rpc.quickstart.Test$Error');
assert(e.message === 'message');
assert(e.stack === 'com.alipay.sofa.rpc.quickstart.Test$Error: message\n at com.alipay.sofa.rpc.quickstart.Test.main (Test.java:30)');
});
});
});
44 changes: 22 additions & 22 deletions test/v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('hessian v1', function () {
tests.forEach(function (t, idx) {
assert.throws(function () {
var buf = encoder.writeInt(t);
}, 'hessian writeInt expect input type is `int32`, but got `number` : ' + tests[idx] + ' ');
}, null, 'hessian writeInt expect input type is `int32`, but got `number` : ' + tests[idx] + ' ');
});
});

Expand All @@ -87,7 +87,7 @@ describe('hessian v1', function () {
tests.forEach(function (t) {
assert.throws(function () {
decoder.init(t[0]).readInt();
}, t[1]);
}, null, t[1]);
});
});
});
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('hessian v1', function () {
tests.forEach(function (t) {
assert.throws(function () {
decoder.init(t[0]).readLong();
}, t[1]);
}, null, t[1]);
});
});
});
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('hessian v1', function () {
tests.forEach(function (t) {
assert.throws(function () {
decoder.init(t[0]).readDouble();
}, t[1]);
}, null, t[1]);
});
});
});
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('hessian v1', function () {
tests.forEach(function (t) {
assert.throws(function () {
decoder.init(t[0]).readDate();
}, t[1]);
}, null, t[1]);
});
});
});
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('hessian v1', function () {
tests.forEach(function (t) {
assert.throws(function () {
var buf = decoder.init(t[0]).readBytes();
}, t[1]);
}, null, t[1]);
});
});

Expand All @@ -247,16 +247,16 @@ describe('hessian v1', function () {
it('should write type error', function () {
assert.throws(function () {
encoder.writeBytes();
}, 'hessian writeBytes expect input type is `buffer`, but got `undefined` : undefined ');
}, null, 'hessian writeBytes expect input type is `buffer`, but got `undefined` : undefined ');
assert.throws(function () {
encoder.writeBytes('');
}, 'hessian writeBytes expect input type is `buffer`, but got `string` : "" ');
}, null, 'hessian writeBytes expect input type is `buffer`, but got `string` : "" ');
assert.throws(function () {
encoder.writeBytes(null);
}, 'hessian writeBytes expect input type is `buffer`, but got `object` : null ');
}, null, 'hessian writeBytes expect input type is `buffer`, but got `object` : null ');
assert.throws(function () {
encoder.writeBytes(100);
}, 'hessian writeBytes expect input type is `buffer`, but got `number` : 100 ');
}, null, 'hessian writeBytes expect input type is `buffer`, but got `number` : 100 ');
});

it('should write and read empty bytes', function () {
Expand Down Expand Up @@ -294,24 +294,24 @@ describe('hessian v1', function () {
tests.forEach(function (t) {
assert.throws(function () {
var buf = decoder.init(t[0]).readString();
}, t[1]);
}, null, t[1]);
});
});

it('should write type error', function () {
assert.throws(function () {
encoder.writeString();
}, 'hessian writeString expect input type is `string`, but got `undefined` : undefined ');
}, null, 'hessian writeString expect input type is `string`, but got `undefined` : undefined ');
// v0.10.28 return [1,2,3,4,5]
// (function () {
// encoder.writeString(new Buffer([1,2,3,4,5]));
// }).should.throw('hessian writeString expect input type is `string`, but got `object` : {"type":"Buffer","data":[1,2,3,4,5]} ');
assert.throws(function () {
encoder.writeString(null);
}, 'hessian writeString expect input type is `string`, but got `object` : null ');
}, null, 'hessian writeString expect input type is `string`, but got `object` : null ');
assert.throws(function () {
encoder.writeString(100);
}, 'hessian writeString expect input type is `string`, but got `number` : 100 ');
}, null, 'hessian writeString expect input type is `string`, but got `number` : 100 ');
});

it('should string length equal MAX_CHAR_TRUNK_SIZE work', function () {
Expand Down Expand Up @@ -457,13 +457,13 @@ describe('hessian v1', function () {
it('should write type error', function () {
assert.throws(function () {
encoder.writeObject('123');
}, 'hessian writeObject / writeMap expect input type is `object`, but got `string` : "123" ');
}, null, 'hessian writeObject / writeMap expect input type is `object`, but got `string` : "123" ');
assert.throws(function () {
encoder.writeObject(1.111);
}, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 1.111 ');
}, null, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 1.111 ');
assert.throws(function () {
encoder.writeObject(100);
}, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 100 ');
}, null, 'hessian writeObject / writeMap expect input type is `object`, but got `number` : 100 ');
});
});

Expand Down Expand Up @@ -539,16 +539,16 @@ describe('hessian v1', function () {
it('should write type error', function () {
assert.throws(function () {
encoder.writeArray();
}, 'hessian writeArray input type invalid');
}, null, 'hessian writeArray input type invalid');
assert.throws(function () {
encoder.writeArray('123');
}, 'hessian writeArray input type invalid');
}, null, 'hessian writeArray input type invalid');
assert.throws(function () {
encoder.writeArray(1.111);
}, 'hessian writeArray input type invalid');
}, null, 'hessian writeArray input type invalid');
assert.throws(function () {
encoder.writeArray(100);
}, 'hessian writeArray input type invalid');
}, null, 'hessian writeArray input type invalid');
});
});

Expand Down Expand Up @@ -615,7 +615,7 @@ describe('hessian v1', function () {
var buf = new Buffer([0x50, 0x11]);
assert.throws(function() {
hessian.decode(buf);
}, 'hessian read got an unexpect code: 0x50');
}, null, 'hessian read got an unexpect code: 0x50');
});
});
});