From 63fb6f8b0208cb948ee29f8b2819c16cd5f95bf9 Mon Sep 17 00:00:00 2001 From: Max Hauser Date: Tue, 30 Apr 2024 09:40:48 +0200 Subject: [PATCH] if alias points to nowhere or object does not exist (#2709) - on getState this means getting a non-existing state - so return null instead of throwing an error - closes #2611 --- packages/adapter/src/lib/adapter/adapter.ts | 15 +++++++-------- packages/controller/test/lib/testAliases.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/adapter/src/lib/adapter/adapter.ts b/packages/adapter/src/lib/adapter/adapter.ts index f99d75bf8..fc55ac26d 100644 --- a/packages/adapter/src/lib/adapter/adapter.ts +++ b/packages/adapter/src/lib/adapter/adapter.ts @@ -7823,7 +7823,7 @@ export class AdapterClass extends EventEmitter { return tools.maybeCallbackWithError(callback, e); } } else { - this._logger.warn(`${this.namespaceLog} ${`Alias ${fixedId} has no target 2`}`); + this._logger.warn(`${this.namespaceLog} Alias ${fixedId} has no target 2`); return tools.maybeCallbackWithError(callback, `Alias ${fixedId} has no target`); } } else { @@ -8578,7 +8578,7 @@ export class AdapterClass extends EventEmitter { callback ); } else { - this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 4`); + this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 3`); return tools.maybeCallbackWithError(callback, `Alias ${id} has no target`); } } else { @@ -8660,7 +8660,7 @@ export class AdapterClass extends EventEmitter { callback ); } else { - this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 5`); + this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 4`); return tools.maybeCallbackWithError(callback, `Alias ${id} has no target`); } } else { @@ -8939,7 +8939,6 @@ export class AdapterClass extends EventEmitter { } if (id.startsWith(ALIAS_STARTS_WITH)) { - // TODO: optimize alias GET performance if (obj?.common?.alias?.id) { // id can be string or can have attribute id.read const aliasId = tools.isObject(obj.common.alias.id) ? obj.common.alias.id.read : obj.common.alias.id; @@ -8991,8 +8990,8 @@ export class AdapterClass extends EventEmitter { ); } } else { - this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 8`); - return tools.maybeCallbackWithError(callback, `Alias ${id} has no target`); + // alias object non-existing or points to nowhere -> handle it like a non-existing state + return tools.maybeCallbackWithError(callback, null, null); } } else { if (this.oStates && this.oStates[id]) { @@ -9651,8 +9650,8 @@ export class AdapterClass extends EventEmitter { } } else if (aliasObj && aliasObj.type === 'state') { // if state and no id given -> if no state just ignore it - this._logger.warn(`${this.namespaceLog} Alias ${aliasObj._id} has no target 12`); - return tools.maybeCallbackWithError(callback, new Error(`Alias ${aliasObj._id} has no target 12`)); + this._logger.warn(`${this.namespaceLog} Alias ${aliasObj._id} has no target 5`); + return tools.maybeCallbackWithError(callback, new Error(`Alias ${aliasObj._id} has no target`)); } else { return tools.maybeCallback(callback); } diff --git a/packages/controller/test/lib/testAliases.ts b/packages/controller/test/lib/testAliases.ts index 1740d55f4..bfc99b03e 100644 --- a/packages/controller/test/lib/testAliases.ts +++ b/packages/controller/test/lib/testAliases.ts @@ -1093,4 +1093,12 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont context.adapter.getForeignStateAsync(nonAliasId, { user: 'system.user.userD' }) ).to.be.eventually.rejectedWith('permissionError', 'Should have thrown a permission error'); }); + + it(testName + 'Non-existing alias should return a null value just like other state', async () => { + const normalState = await context.adapter.getForeignStateAsync(`${gid}.isNotExisting`); + const aliasState = await context.adapter.getForeignStateAsync(`${gAliasID}.isNotExisting`); + + expect(normalState).to.be.null; + expect(aliasState).to.be.equal(normalState); + }); }