From ba37b28fb51ff98ad5628ce57dc4f591ca30c992 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sat, 13 Jan 2024 23:59:00 +0530 Subject: [PATCH] Improve batched error handling. Catch can't connect to server error Break out of batch processing when unable to connect to server or when requests throttled by server --- src/interface/obsidian/src/utils.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index 8a75d6e6..1f309ff6 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -55,9 +55,8 @@ export async function updateContentIndex(vault: Vault, setting: KhojSetting, las } // Iterate through all indexable files in vault, 1000 at a time - let batchResponseSuccess = true; - let batchResponseThrottled = false; - for (let i = 0; i < fileData.length && !batchResponseThrottled; i += 1000) { + let error_message = null; + for (let i = 0; i < fileData.length; i += 1000) { const filesGroup = fileData.slice(i, i + 1000); const formData = new FormData(); filesGroup.forEach(fileItem => { formData.append('files', fileItem.blob, fileItem.path) }); @@ -71,15 +70,20 @@ export async function updateContentIndex(vault: Vault, setting: KhojSetting, las }); if (!response.ok) { - batchResponseSuccess = false; - batchResponseThrottled = response.status === 429; + if (response.status === 429) { + error_message = `❗️Failed to sync your content with Khoj server. Requests were throttled. Upgrade your subscription or try again later.`; + break; + } else if (response.status === 404) { + error_message = `❗️Could not connect to Khoj server. Ensure you can connect to it.`; + break; + } else { + error_message = `❗️Failed to sync your content with Khoj server. Raise issue on Khoj Discord or Github\nError: ${response.statusText}`; + } } } - if (batchResponseThrottled) { - new Notice(`❗️Failed to update Khoj content index. Requests were throttled. Upgrade your subscription or try again later.`); - } else if (!batchResponseSuccess) { - new Notice(`❗️Failed to update Khoj content index. Ensure Khoj server connected or raise issue on Khoj Discord/Github\nError: ${response.statusText}`); + if (error_message) { + new Notice(error_message); } else { console.log(`✅ Refreshed Khoj content index. Updated: ${countOfFilesToIndex} files, Deleted: ${countOfFilesToDelete} files.`); }