Skip to content

Commit bd54d5b

Browse files
committed
fix(globalState): log update() errors
1 parent 8c7ec00 commit bd54d5b

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

packages/core/src/codewhisperer/models/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class CodeSuggestionsState {
9292
async toggleSuggestions() {
9393
const autoTriggerEnabled = this.isSuggestionsEnabled()
9494
const toSet: boolean = !autoTriggerEnabled
95-
await globals.globalState.tryUpdate('CODEWHISPERER_AUTO_TRIGGER_ENABLED', toSet)
95+
await globals.globalState.update('CODEWHISPERER_AUTO_TRIGGER_ENABLED', toSet)
9696
this.#onDidChangeState.fire(toSet)
9797
return toSet
9898
}
@@ -131,7 +131,7 @@ export class CodeScansState {
131131
async toggleScans() {
132132
const autoScansEnabled = this.isScansEnabled()
133133
const toSet: boolean = !autoScansEnabled
134-
await globals.globalState.tryUpdate('CODEWHISPERER_AUTO_SCANS_ENABLED', toSet)
134+
await globals.globalState.update('CODEWHISPERER_AUTO_SCANS_ENABLED', toSet)
135135
this.#onDidChangeState.fire(toSet)
136136
return toSet
137137
}

packages/core/src/codewhisperer/util/customizationUtil.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const setSelectedCustomization = async (customization: Customization) =>
134134
selectedCustomizationObj[AuthUtil.instance.conn.label] = customization
135135
getLogger().debug(`Selected customization ${customization.name} for ${AuthUtil.instance.conn.label}`)
136136

137-
await globals.globalState.tryUpdate('CODEWHISPERER_SELECTED_CUSTOMIZATION', selectedCustomizationObj)
137+
await globals.globalState.update('CODEWHISPERER_SELECTED_CUSTOMIZATION', selectedCustomizationObj)
138138
vsCodeState.isFreeTierLimitReached = false
139139
await Commands.tryExecute('aws.amazonq.refreshStatusBar')
140140
}
@@ -161,15 +161,15 @@ export const setPersistedCustomizations = async (customizations: Customization[]
161161
{}
162162
)
163163
persistedCustomizationsObj[AuthUtil.instance.conn.label] = customizations
164-
await globals.globalState.tryUpdate('CODEWHISPERER_PERSISTED_CUSTOMIZATIONS', persistedCustomizationsObj)
164+
await globals.globalState.update('CODEWHISPERER_PERSISTED_CUSTOMIZATIONS', persistedCustomizationsObj)
165165
}
166166

167167
export const getNewCustomizationsAvailable = () => {
168168
return globals.globalState.tryGet('aws.amazonq.codewhisperer.newCustomizations', Number, 0)
169169
}
170170

171171
export const setNewCustomizationsAvailable = async (num: number) => {
172-
await globals.globalState.tryUpdate('aws.amazonq.codewhisperer.newCustomizations', num)
172+
await globals.globalState.update('aws.amazonq.codewhisperer.newCustomizations', num)
173173
vsCodeState.isFreeTierLimitReached = false
174174
}
175175

packages/core/src/codewhisperer/views/lineAnnotationController.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class LineAnnotationController implements vscode.Disposable {
308308
async dismissTutorial() {
309309
this._currentState = new EndState()
310310
await setContext('aws.codewhisperer.tutorial.workInProgress', false)
311-
await globals.globalState.tryUpdate(inlinehintKey, this._currentState.id)
311+
await globals.globalState.update(inlinehintKey, this._currentState.id)
312312
}
313313

314314
private async onActiveLinesChanged(e: LinesChangeEvent) {
@@ -422,7 +422,7 @@ export class LineAnnotationController implements vscode.Disposable {
422422

423423
decorationOptions.range = range
424424

425-
await globals.globalState.tryUpdate(inlinehintKey, this._currentState.id)
425+
await globals.globalState.update(inlinehintKey, this._currentState.id)
426426
await setContext('aws.codewhisperer.tutorial.workInProgress', true)
427427
editor.setDecorations(this.cwLineHintDecoration, [decorationOptions])
428428
}

packages/core/src/shared/filesystemUtilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ export async function getDefaultDownloadPath(): Promise<string> {
267267
export async function setDefaultDownloadPath(downloadPath: string) {
268268
try {
269269
if (await fs.existsDir(downloadPath)) {
270-
globals.globalState.tryUpdate('aws.downloadPath', downloadPath)
270+
await globals.globalState.update('aws.downloadPath', downloadPath)
271271
} else {
272-
globals.globalState.tryUpdate('aws.downloadPath', path.dirname(downloadPath))
272+
await globals.globalState.update('aws.downloadPath', path.dirname(downloadPath))
273273
}
274274
} catch (err) {
275275
getLogger().error('Error while setting "aws.downloadPath"', err as Error)

packages/core/src/shared/globalState.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,25 @@ export class GlobalState implements vscode.Memento {
118118
}
119119
}
120120

121-
/** Asynchronously updates globalState, or logs an error on failure. */
122-
tryUpdate(key: globalKey, value: any) {
123-
return this.memento.update(key, value).then(
124-
undefined, // TODO: log.debug() ?
125-
(e) => {
126-
getLogger().error('GlobalState: failed to set "%s": %s', key, (e as Error).message)
127-
}
128-
)
121+
/**
122+
* Asynchronously updates globalState, or logs an error on failure.
123+
*
124+
* Only for callers that cannot `await` or don't care about errors and race conditions. Prefer
125+
* `await update()` where possible.
126+
*/
127+
tryUpdate(key: globalKey, value: any): void {
128+
this.update(key, value).then(undefined, () => {
129+
// Errors are logged by update().
130+
})
129131
}
130132

131-
update(key: globalKey, value: any): Thenable<void> {
132-
return this.memento.update(key, value)
133+
async update(key: globalKey, value: any): Promise<void> {
134+
try {
135+
await this.memento.update(key, value)
136+
} catch (e) {
137+
getLogger().error('GlobalState: failed to set "%s": %s', key, (e as Error).message)
138+
throw e
139+
}
133140
}
134141

135142
clear() {

0 commit comments

Comments
 (0)