Load obsidian plugin even if fail to connect to backend but show warning

- Previously the plugin would not load if cannot connect to Khoj backend
  - Silently failing to load with no reason provided is not helpful
- Load plugin to allow user to fix the Khoj URL in their plugin setting
- Show reason for khoj plugin not working. More helpful than failing silently
This commit is contained in:
Debanjum Singh Solanky 2023-01-10 17:03:50 -03:00
parent aa22d83172
commit 768e874185

View file

@ -13,16 +13,23 @@ export async function configureKhojBackend(setting: KhojSetting) {
let mdInVault = `${setting.obsidianVaultPath}/**/*.md`;
let khojConfigUrl = `${setting.khojUrl}/api/config/data`;
// Load khoj app config from backend API, Update Markdown config and save
let khoj_configured = await request(khojConfigUrl)
.then(response => response !== "null");
// Check if khoj backend is configured, show error if backend is not running
let khoj_already_configured = await request(khojConfigUrl)
.then(response => {
return response !== "null"
})
.catch(error => {
new Notice(`Ensure Khoj backend is running and Khoj URL is pointing to it in the Khoj plugin settings.\n\n${error}`);
})
// Short-circuit configuring khoj if unable to connect to khoj backend
if (khoj_already_configured === null) return;
// Get current config if khoj backend configured, else get default config from khoj backend
await request(khoj_configured ? khojConfigUrl : `${khojConfigUrl}/default`)
await request(khoj_already_configured ? khojConfigUrl : `${khojConfigUrl}/default`)
.then(response => JSON.parse(response))
.then(data => {
// If khoj backend not configured yet
if (!khoj_configured) {
if (!khoj_already_configured) {
// Create khoj content-type config with only markdown configured
let khojObsidianPluginPath = `${setting.obsidianVaultPath}/${this.app.vault.configDir}/plugins/khoj/`;
data["content-type"] = {
@ -70,9 +77,10 @@ export async function configureKhojBackend(setting: KhojSetting) {
updateKhojBackend(setting.khojUrl, data);
console.log(`Khoj: Updated markdown config in khoj backend config:\n${JSON.stringify(data["content-type"]["markdown"])}`)
}
new Notice(`✅ Successfully Setup Khoj`);
})
.catch(error => {
new Notice(`Ensure Khoj backend is running and Khoj URL is correct in the Khoj plugin settings.\nError: ${error}`);
new Notice(`Failed to configure Khoj backend. Contact developer on Github. \n\nError: ${error}`);
})
}