Skip to content

Commit

Permalink
feat(settings): log without throwing if default value is provided
Browse files Browse the repository at this point in the history
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<Object>('files.exclude', Number, 42)

Solution:
Copy the logic from `AnonymousSettings.get()`.

TODO: see if we can de-duplicate the logic.
  • Loading branch information
justinmk3 committed Aug 24, 2023
1 parent 33e0b8d commit 16812e0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/shared/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ export class Settings {
public get<T>(key: string, type: TypeConstructor<T>): T | undefined
public get<T>(key: string, type: TypeConstructor<T>, defaultValue: T): T
public get<T>(key: string, type?: TypeConstructor<T>, 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
}
}

/**
Expand Down

0 comments on commit 16812e0

Please sign in to comment.