This is an automated email from the ASF dual-hosted git repository. isapego pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ignite-nodejs-thin-client.git
commit fb95ebd2d6295638e0660aeadbfc336fe936c0f4 Author: ekaterina-nbl <[email protected]> AuthorDate: Mon Oct 29 15:49:33 2018 +0300 IGNITE-10022: JS, PHP thin clients: a more meaningful exception when ENUM type is not registered This closes #5187 --- lib/EnumItem.js | 17 ++++++++++---- lib/Errors.js | 12 ++++++++++ lib/internal/BinaryUtils.js | 4 ++++ spec/TestingHelper.js | 7 ++++++ spec/cache/CachePutGetDiffTypes.spec.js | 39 +++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/EnumItem.js b/lib/EnumItem.js index 1d1725e..5e80da9 100644 --- a/lib/EnumItem.js +++ b/lib/EnumItem.js @@ -17,6 +17,7 @@ 'use strict'; +const Util = require('util'); const ArgumentChecker = require('./internal/ArgumentChecker'); const Errors = require('./Errors'); @@ -157,14 +158,18 @@ class EnumItem { * @ignore */ async _write(communicator, buffer) { + const type = await this._getType(communicator, this._typeId); + if (!type || !type._isEnum) { + throw Errors.IgniteClientError.enumSerializationError( + true, Util.format('enum type id "%d" is not registered', this._typeId)); + } buffer.writeInteger(this._typeId); if (this._ordinal !== null) { buffer.writeInteger(this._ordinal); return; } else if (this._name !== null || this._value !== null) { - const type = await this._getType(communicator, this._typeId); - if (type._isEnum && type._enumValues) { + if (type._enumValues) { for (let i = 0; i < type._enumValues.length; i++) { if (this._name === type._enumValues[i][0] || this._value === type._enumValues[i][1]) { @@ -185,8 +190,12 @@ class EnumItem { this._typeId = buffer.readInteger(); this._ordinal = buffer.readInteger(); const type = await this._getType(communicator, this._typeId); - if (!type._isEnum || !type._enumValues || type._enumValues.length <= this._ordinal) { - throw new Errors.IgniteClientError('EnumItem can not be deserialized: type mismatch'); + if (!type || !type._isEnum) { + throw Errors.IgniteClientError.enumSerializationError( + false, Util.format('enum type id "%d" is not registered', this._typeId)); + } + else if (!type._enumValues || type._enumValues.length <= this._ordinal) { + throw Errors.IgniteClientError.enumSerializationError(false, 'type mismatch'); } this._name = type._enumValues[this._ordinal][0]; this._value = type._enumValues[this._ordinal][1]; diff --git a/lib/Errors.js b/lib/Errors.js index 57a7a8c..89baf38 100644 --- a/lib/Errors.js +++ b/lib/Errors.js @@ -83,6 +83,18 @@ class IgniteClientError extends Error { } return new IgniteClientError(msg); } + + /** + * EnumItem serialization/deserialization errors. + * @ignore + */ + static enumSerializationError(serialize, message = null) { + let msg = serialize ? 'Enum item can not be serialized' : 'Enum item can not be deserialized'; + if (message) { + msg = msg + ': ' + message; + } + return new IgniteClientError(msg); + } } /** diff --git a/lib/internal/BinaryUtils.js b/lib/internal/BinaryUtils.js index 2619df7..fe1e403 100644 --- a/lib/internal/BinaryUtils.js +++ b/lib/internal/BinaryUtils.js @@ -497,6 +497,10 @@ class BinaryUtils { expectedTypeCode === BinaryUtils.TYPE_CODE.COMPLEX_OBJECT) { return; } + else if (expectedTypeCode === BinaryUtils.TYPE_CODE.ENUM && + actualTypeCode === BinaryUtils.TYPE_CODE.BINARY_ENUM) { + return; + } else if (actualTypeCode !== expectedTypeCode) { throw Errors.IgniteClientError.typeCastError(actualTypeCode, expectedTypeCode); } diff --git a/spec/TestingHelper.js b/spec/TestingHelper.js index 79a5336..78df0cb 100644 --- a/spec/TestingHelper.js +++ b/spec/TestingHelper.js @@ -232,6 +232,13 @@ class TestingHelper { TestingHelper.checkError(error, Errors.IgniteClientError, done) } + static checkEnumItemSerializationError(error, done) { + if (!(error instanceof Errors.IgniteClientError) || + error.message.indexOf('Enum item can not be serialized') < 0) { + done.fail('unexpected error: ' + error); + } + } + static checkError(error, errorType, done) { if (!(error instanceof errorType)) { done.fail('unexpected error: ' + error); diff --git a/spec/cache/CachePutGetDiffTypes.spec.js b/spec/cache/CachePutGetDiffTypes.spec.js index 28a9ae3..a6e1bba 100644 --- a/spec/cache/CachePutGetDiffTypes.spec.js +++ b/spec/cache/CachePutGetDiffTypes.spec.js @@ -543,6 +543,45 @@ describe('cache put get test suite >', () => { catch(error => done.fail(error)); }); + it('put enum items', (done) => { + Promise.resolve(). + then(async () => { + const fakeTypeId = 12345; + const enumItem1 = new EnumItem(fakeTypeId); + enumItem1.setOrdinal(1); + await putEnumItem(enumItem1, null, done); + await putEnumItem(enumItem1, ObjectType.PRIMITIVE_TYPE.ENUM, done); + const enumItem2 = new EnumItem(fakeTypeId); + enumItem2.setName('name'); + await putEnumItem(enumItem2, null, done); + await putEnumItem(enumItem2, ObjectType.PRIMITIVE_TYPE.ENUM, done); + const enumItem3 = new EnumItem(fakeTypeId); + enumItem3.setValue(2); + await putEnumItem(enumItem3, null, done); + await putEnumItem(enumItem3, ObjectType.PRIMITIVE_TYPE.ENUM, done); + }). + then(done). + catch(error => done.fail(error)); + }); + + async function putEnumItem(value, valueType, done) { + const cache = igniteClient.getCache(CACHE_NAME). + setKeyType(null). + setValueType(valueType); + const key = new Date(); + // Enums registration is not supported by the client, therefore put EnumItem must throw IgniteClientError + try { + await cache.put(key, value); + done.fail('put EnumItem must throw IgniteClientError'); + } + catch (err) { + TestingHelper.checkEnumItemSerializationError(err, done); + } + finally { + await cache.removeAll(); + } + } + async function putGetPrimitiveValues(keyType, valueType, key, value, modificator) { const cache = await igniteClient.getCache(CACHE_NAME). setKeyType(keyType).
