Show nicer message when Appwrite project can't be found (#14)

* Show nicer message when Appwrite project can't be found

* fix lint errors
This commit is contained in:
Alex Weininger 2021-05-14 13:00:10 -07:00 committed by GitHub
parent f8494fa268
commit 797b3d74ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 16 deletions

View file

@ -34,21 +34,35 @@ export class HealthTreeItemProvider implements vscode.TreeDataProvider<vscode.Tr
// get children for root // get children for root
if (element === undefined) { if (element === undefined) {
const health = await promiseWithTimeout<AppwriteHealth | undefined>(10000, async () => await healthClient?.checkup(), 'Health request timed out'); try {
if (health === undefined) { const health = await promiseWithTimeout<AppwriteHealth | undefined>(
return []; 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 []; return [];
} }

View file

@ -1,7 +1,9 @@
import { window } from 'vscode';
export const promiseWithTimeout = <T>(timeoutMs: number, promise: () => Promise<T>, failureMessage?: string): Promise<T> => { export const promiseWithTimeout = <T>(timeoutMs: number, promise: () => Promise<T>, failureMessage?: string): Promise<T> => {
let timeoutHandle: NodeJS.Timeout; let timeoutHandle: NodeJS.Timeout;
const timeoutPromise = new Promise<never>((resolve, reject) => { const timeoutPromise = new Promise<never>(() => {
timeoutHandle = setTimeout(() => reject(new Error(failureMessage)), timeoutMs); timeoutHandle = setTimeout(() => window.showErrorMessage(failureMessage ?? 'Request timed out'), timeoutMs);
}); });
return Promise.race([promise(), timeoutPromise]).then((result) => { return Promise.race([promise(), timeoutPromise]).then((result) => {