Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications about network issues should only be displayed once #109

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { LanguageToolPluginSettings } from './SettingsTab';

export const logs: string[] = [];

let lastStatus: 'ok' | 'request-failed' | 'request-not-ok' | 'json-parse-error' = 'ok';

export async function getDetectionResult(
text: string,
getSettings: () => LanguageToolPluginSettings,
Expand Down Expand Up @@ -97,24 +99,42 @@ export async function getDetectionResult(
},
});
} catch (e) {
new Notice(`Request to LanguageTool server failed. Please check your connection and LanguageTool server URL`, 5000);
const status = 'request-failed';
if (lastStatus !== status || !settings.shouldAutoCheck) {
new Notice(`Request to LanguageTool server failed. Please check your connection and LanguageTool server URL`, 0);
lastStatus = status;
}
return Promise.reject(e);
}

if (!res.ok) {
const status = 'request-not-ok';
await pushLogs(res, settings);
new Notice(`Request to LanguageTool failed\n${res.statusText}Check Plugin Settings for Logs`, 5000);
if (lastStatus !== status || !settings.shouldAutoCheck) {
new Notice(`Request to LanguageTool failed\n${res.statusText}Check Plugin Settings for Logs`, 0);
lastStatus = status;
}
return Promise.reject(new Error(`unexpected status ${res.status}, see network tab`));
}

let body: LanguageToolApi;
try {
body = await res.json();
} catch (e) {
new Notice(`Error processing response from LanguageTool server`, 5000);
const status = 'json-parse-error';
if (lastStatus !== status || !settings.shouldAutoCheck) {
new Notice(`Error processing response from LanguageTool server`, 0);
lastStatus = status;
}
return Promise.reject(e);
}

const status = 'ok';
if (lastStatus !== status || !settings.shouldAutoCheck) {
new Notice(`LanguageTool detection restored`, 5000);
lastStatus = status;
}

return body;
}

Expand Down
Loading