refactor: Change the setting update validation, so we don't print unnecessary warnings (#372)

This commit is contained in:
Mykola Mokhnach
2019-12-09 15:58:41 +01:00
committed by GitHub
parent 9c9f03fc4c
commit 4e47102f56
+16 -14
View File
@@ -73,23 +73,25 @@ class DeviceSettings {
// calls updateSettings from implementing driver every time a setting is changed.
async update (newSettings) {
if (!_.isObject(newSettings)) {
throw new Error('Settings update should be called with valid JSON');
if (!_.isPlainObject(newSettings)) {
throw new Error(`Settings update should be called with valid JSON. Got ` +
`${JSON.stringify(newSettings)} instead`);
}
for (let prop of _.keys(newSettings)) {
if (_.isUndefined(this._settings[prop])) {
log.warn(`Didn't know about setting '${prop}'. Are you sure you ` +
`spelled it correctly? Proceeding anyway. Valid settings: ${_.keys(this._settings)}`);
}
if (this._settings[prop] !== newSettings[prop]) {
// update setting only when there is updateSettings defined.
if (this.onSettingsUpdate) {
await this.onSettingsUpdate(prop, newSettings[prop], this._settings[prop]);
this._settings[prop] = newSettings[prop];
} else {
log.errorAndThrow('Unable to update settings; onSettingsUpdate method not found');
for (const prop of _.keys(newSettings)) {
if (!_.isUndefined(this._settings[prop])) {
if (this._settings[prop] === newSettings[prop]) {
log.debug(`The value of '${prop}' setting did not change. Skipping the update for it`);
continue;
}
}
// update setting only when there is updateSettings defined.
if (_.isFunction(this.onSettingsUpdate)) {
await this.onSettingsUpdate(prop, newSettings[prop], this._settings[prop]);
this._settings[prop] = newSettings[prop];
} else {
log.errorAndThrow(`Unable to update settings; ` +
`onSettingsUpdate method not found on '${this.constructor.name}'`);
}
}
}