diff --git a/src/tree/health/HealthTreeItemProvider.ts b/src/tree/health/HealthTreeItemProvider.ts index bbb95b0..b5f7403 100644 --- a/src/tree/health/HealthTreeItemProvider.ts +++ b/src/tree/health/HealthTreeItemProvider.ts @@ -34,21 +34,35 @@ export class HealthTreeItemProvider implements vscode.TreeDataProvider(10000, async () => await healthClient?.checkup(), 'Health request timed out'); - if (health === undefined) { - return []; + try { + const health = await promiseWithTimeout( + 10000, + async () => { + try { + return await healthClient?.checkup(); + } catch (e) { + vscode.window.showErrorMessage('Could not connect to Appwrite project'); + } + }, + "Health request timed out" + ); + if (health === undefined) { + return []; + } + ext.outputChannel?.append(JSON.stringify(health, null, 4)); + const healthItems = Object.entries(health).map(([service, status]) => { + return new HealthTreeItem(service, status, healthTooltips[service as keyof AppwriteHealth]); + }); + this.lastChecked = new Date(); + return [ + { + label: `Updated at ${dayjs(this.lastChecked).format("LTS")}`, + }, + ...healthItems, + ]; + } catch (e) { + // } - ext.outputChannel?.append(JSON.stringify(health, null, 4)); - const healthItems = Object.entries(health).map(([service, status]) => { - return new HealthTreeItem(service, status, healthTooltips[service as keyof AppwriteHealth]); - }); - this.lastChecked = new Date(); - return [ - { - label: `Updated at ${dayjs(this.lastChecked).format("LTS")}`, - }, - ...healthItems, - ]; } return []; } diff --git a/src/utils/promiseWithTimeout.ts b/src/utils/promiseWithTimeout.ts index 321664a..794a39f 100644 --- a/src/utils/promiseWithTimeout.ts +++ b/src/utils/promiseWithTimeout.ts @@ -1,7 +1,9 @@ +import { window } from 'vscode'; + export const promiseWithTimeout = (timeoutMs: number, promise: () => Promise, failureMessage?: string): Promise => { let timeoutHandle: NodeJS.Timeout; - const timeoutPromise = new Promise((resolve, reject) => { - timeoutHandle = setTimeout(() => reject(new Error(failureMessage)), timeoutMs); + const timeoutPromise = new Promise(() => { + timeoutHandle = setTimeout(() => window.showErrorMessage(failureMessage ?? 'Request timed out'), timeoutMs); }); return Promise.race([promise(), timeoutPromise]).then((result) => {