Skip to content

Commit

Permalink
if alias points to nowhere or object does not exist (#2709)
Browse files Browse the repository at this point in the history
- on getState this means getting a non-existing state
- so return null instead of throwing an error
- closes #2611
  • Loading branch information
foxriver76 authored Apr 30, 2024
1 parent 656a52f commit 63fb6f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/adapter/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/controller/test/lib/testAliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

0 comments on commit 63fb6f8

Please sign in to comment.