Skip to content

Commit

Permalink
* enhance feature to get clearer error returns
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed May 27, 2022
1 parent 720adfb commit f150a1c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
54 changes: 38 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,10 @@ function deleteStateAll(msg) {
}

function storeStatePushData(id, state, applyRules) {
if (!state || typeof state !== 'object') {
throw new Error(`State ${JSON.stringify(state)} for ${id} is not valid`);
}

let pushFunc = applyRules ? pushHistory : pushHelper;
if (!history[id] || !history[id][adapter.namespace]) {
if (applyRules) {
Expand All @@ -1825,32 +1829,50 @@ async function storeState(msg) {
}

let id;
try {
if (Array.isArray(msg.message)) {
adapter.log.debug(`storeState: store ${msg.message.length} states for multiple ids`);
for (let i = 0; i < msg.message.length; i++) {
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
let errors = [];
let successCount = 0;
if (Array.isArray(msg.message)) {
adapter.log.debug(`storeState: store ${msg.message.length} states for multiple ids`);
for (let i = 0; i < msg.message.length; i++) {
id = aliasMap[msg.message[i].id] ? aliasMap[msg.message[i].id] : msg.message[i].id;
try {
storeStatePushData(id, msg.message[i].state, msg.message.rules);
successCount++;
} catch (err) {
errors.push(err.message);
}
} else if (msg.message.state && Array.isArray(msg.message.state)) {
adapter.log.debug(`storeState: store ${msg.message.state.length} states for ${msg.message.id}`);
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
for (let j = 0; j < msg.message.state.length; j++) {
}
} else if (msg.message.state && Array.isArray(msg.message.state)) {
adapter.log.debug(`storeState: store ${msg.message.state.length} states for ${msg.message.id}`);
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
for (let j = 0; j < msg.message.state.length; j++) {
try {
storeStatePushData(id, msg.message.state[j], msg.message.rules);
successCount++;
} catch (err) {
errors.push(err.message);
}
} else {
adapter.log.debug(`storeState: store 1 state for ${msg.message.id}`);
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
}
} else {
adapter.log.debug(`storeState: store 1 state for ${msg.message.id}`);
id = aliasMap[msg.message.id] ? aliasMap[msg.message.id] : msg.message.id;
try {
storeStatePushData(id, msg.message.state, msg.message.rules);
successCount++;
} catch (err) {
errors.push(err.message);
}
} catch (err) {
adapter.log.warn(`storeState: ${err.message}`);
}
if (error.length) {
adapter.log.warn(`storeState executed with ${errors.length} errors: ${errors.join(', ')}`);
return adapter.sendTo(msg.from, msg.command, {
error: err.message
error: `${errors.length} errors happened while storing data`,
errors: errors,
successCount
}, msg.callback);
}

adapter.sendTo(msg.from, msg.command, {success: true}, msg.callback);
adapter.sendTo(msg.from, msg.command, {success: true, successCount}, msg.callback);
}

function enableHistory(msg) {
Expand Down
9 changes: 7 additions & 2 deletions test/lib/testcases.js
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ function register(it, expect, sendTo, adapterShortName, writeNulls, assumeExisti
]
}, function (result) {
expect(result.success).to.be.true;
expect(result.successCount).to.be.equal(3);

setTimeout( () => {
sendTo(instanceName, 'getHistory', {
Expand Down Expand Up @@ -1100,11 +1101,15 @@ function register(it, expect, sendTo, adapterShortName, writeNulls, assumeExisti
state: [
{val: 1, ack: true, ts: customNow2 - 5000},
{val: 2, ack: true, ts: customNow2 - 4000},
{val: 3, ack: true, ts: customNow2 - 3000}
'37'
]
}, function (result) {
expect(result.success).to.be.not.ok;
expect(result.error).to.be.equal(`history not enabled for my.own.unknown.value-${customNow2}, so can not apply the rules as requested`);
expect(result.successCount).to.be.equal(0);
expect(result.error).to.be.equal('3 errors happened while storing data');
expect(Array.isArray(result.errors)).to.be.true;
expect(result.errors[0]).to.be.equal(`history not enabled for my.own.unknown.value-${customNow2}, so can not apply the rules as requested`);
expect(result.errors[2]).to.be.equal(`State "37" for my.own.unknown.value-${customNow2} is not valid`);

done();
});
Expand Down

0 comments on commit f150a1c

Please sign in to comment.