Skip to content

Commit 63fb6f8

Browse files
authored
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
1 parent 656a52f commit 63fb6f8

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

packages/adapter/src/lib/adapter/adapter.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7823,7 +7823,7 @@ export class AdapterClass extends EventEmitter {
78237823
return tools.maybeCallbackWithError(callback, e);
78247824
}
78257825
} else {
7826-
this._logger.warn(`${this.namespaceLog} ${`Alias ${fixedId} has no target 2`}`);
7826+
this._logger.warn(`${this.namespaceLog} Alias ${fixedId} has no target 2`);
78277827
return tools.maybeCallbackWithError(callback, `Alias ${fixedId} has no target`);
78287828
}
78297829
} else {
@@ -8578,7 +8578,7 @@ export class AdapterClass extends EventEmitter {
85788578
callback
85798579
);
85808580
} else {
8581-
this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 4`);
8581+
this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 3`);
85828582
return tools.maybeCallbackWithError(callback, `Alias ${id} has no target`);
85838583
}
85848584
} else {
@@ -8660,7 +8660,7 @@ export class AdapterClass extends EventEmitter {
86608660
callback
86618661
);
86628662
} else {
8663-
this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 5`);
8663+
this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 4`);
86648664
return tools.maybeCallbackWithError(callback, `Alias ${id} has no target`);
86658665
}
86668666
} else {
@@ -8939,7 +8939,6 @@ export class AdapterClass extends EventEmitter {
89398939
}
89408940

89418941
if (id.startsWith(ALIAS_STARTS_WITH)) {
8942-
// TODO: optimize alias GET performance
89438942
if (obj?.common?.alias?.id) {
89448943
// id can be string or can have attribute id.read
89458944
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 {
89918990
);
89928991
}
89938992
} else {
8994-
this._logger.warn(`${this.namespaceLog} Alias ${id} has no target 8`);
8995-
return tools.maybeCallbackWithError(callback, `Alias ${id} has no target`);
8993+
// alias object non-existing or points to nowhere -> handle it like a non-existing state
8994+
return tools.maybeCallbackWithError(callback, null, null);
89968995
}
89978996
} else {
89988997
if (this.oStates && this.oStates[id]) {
@@ -9651,8 +9650,8 @@ export class AdapterClass extends EventEmitter {
96519650
}
96529651
} else if (aliasObj && aliasObj.type === 'state') {
96539652
// if state and no id given -> if no state just ignore it
9654-
this._logger.warn(`${this.namespaceLog} Alias ${aliasObj._id} has no target 12`);
9655-
return tools.maybeCallbackWithError(callback, new Error(`Alias ${aliasObj._id} has no target 12`));
9653+
this._logger.warn(`${this.namespaceLog} Alias ${aliasObj._id} has no target 5`);
9654+
return tools.maybeCallbackWithError(callback, new Error(`Alias ${aliasObj._id} has no target`));
96569655
} else {
96579656
return tools.maybeCallback(callback);
96589657
}

packages/controller/test/lib/testAliases.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,4 +1093,12 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
10931093
context.adapter.getForeignStateAsync(nonAliasId, { user: 'system.user.userD' })
10941094
).to.be.eventually.rejectedWith('permissionError', 'Should have thrown a permission error');
10951095
});
1096+
1097+
it(testName + 'Non-existing alias should return a null value just like other state', async () => {
1098+
const normalState = await context.adapter.getForeignStateAsync(`${gid}.isNotExisting`);
1099+
const aliasState = await context.adapter.getForeignStateAsync(`${gAliasID}.isNotExisting`);
1100+
1101+
expect(normalState).to.be.null;
1102+
expect(aliasState).to.be.equal(normalState);
1103+
});
10961104
}

0 commit comments

Comments
 (0)