From 16812e0b2baba08d472c35e2e940f6931fd6389c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 24 Aug 2023 08:49:40 -0700 Subject: [PATCH] feat(settings): log without throwing if default value is provided Problem: If a user setting has the wrong type, `Settings.instance.get` throws an exception, even if a `defaultValue` is provided. We almost never want that behavior. Example: Settings.instance.get('files.exclude', Number, 42) Solution: Copy the logic from `AnonymousSettings.get()`. TODO: see if we can de-duplicate the logic. --- src/shared/settings.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/shared/settings.ts b/src/shared/settings.ts index b467355a91e..f9210970b40 100644 --- a/src/shared/settings.ts +++ b/src/shared/settings.ts @@ -41,9 +41,18 @@ export class Settings { public get(key: string, type: TypeConstructor): T | undefined public get(key: string, type: TypeConstructor, defaultValue: T): T public get(key: string, type?: TypeConstructor, defaultValue?: T) { - const value = this.getConfig().get(key, defaultValue) + try { + const value = this.getConfig().get(key, defaultValue) + + return !type || value === undefined ? value : cast(value, type) + } catch (e) { + if (arguments.length === 1) { + throw ToolkitError.chain(e, `Failed to read key "${key}"`) + } + getLogger().error(`using default for key "${key}", read failed: %s`, (e as Error).message) - return !type || value === undefined ? value : cast(value, type) + return defaultValue + } } /**