From d66aeeafef53f26c94e0dd9535714c41fe04ee18 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Fri, 6 Jun 2025 19:17:44 +0200 Subject: [PATCH] #467 - Higher tolerance for errors during indexing, show error when result is missing from search --- src/repositories/documents-repository.ts | 15 ++++-------- src/search/search-engine.ts | 21 +++++++++++++---- src/tools/utils.ts | 29 +++++++++++++++++++++++- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/repositories/documents-repository.ts b/src/repositories/documents-repository.ts index 49859d6..c03f6d1 100644 --- a/src/repositories/documents-repository.ts +++ b/src/repositories/documents-repository.ts @@ -1,6 +1,7 @@ import { normalizePath, Notice, TFile } from 'obsidian' import type { IndexedDocument } from '../globals' import { + countError, extractHeadingsFromCache, getAliasesFromMetadata, getTagsFromMetadata, @@ -53,7 +54,7 @@ export class DocumentsRepository { console.warn(`Omnisearch: Error while adding "${path}" to live cache`, e) // Shouldn't be needed, but... this.removeDocument(path) - this.countError() + countError() } } @@ -72,7 +73,7 @@ export class DocumentsRepository { // Only happens if the cache is corrupted if (!document) { console.error('Omnisearch', path, 'cannot be read') - this.countError() + countError() } // The document might be undefined, but this shouldn't stop the search from mostly working @@ -256,13 +257,5 @@ export class DocumentsRepository { } } - private countError(): void { - if (++this.errorsCount >= 3 && !this.errorsWarned) { - this.errorsWarned = true - new Notice( - 'Omnisearch ⚠️ There might be an issue with your cache. You should clean it in Omnisearch settings and restart Obsidian.', - 0 - ) - } - } + } diff --git a/src/search/search-engine.ts b/src/search/search-engine.ts index 3a3bdc0..9d2a41b 100644 --- a/src/search/search-engine.ts +++ b/src/search/search-engine.ts @@ -10,7 +10,12 @@ import { type ResultNote, } from '../globals' -import { chunkArray, logVerbose, removeDiacritics } from '../tools/utils' +import { + chunkArray, + countError, + logVerbose, + removeDiacritics, +} from '../tools/utils' import { Notice } from 'obsidian' import type { Query } from './query' import { sortBy } from 'lodash-es' @@ -323,10 +328,16 @@ export class SearchEngine { if (results.length) logVerbose('First result:', results[0]) const documents = await Promise.all( - results.map( - async result => - await this.plugin.documentsRepository.getDocument(result.id) - ) + results.map(async result => { + const doc = await this.plugin.documentsRepository.getDocument(result.id) + if (!doc) { + console.warn( + `Omnisearch - Note "${result.id}" not in the live cache` + ) + countError(true) + } + return doc + }) ) // If the search query contains quotes, filter out results that don't have the exact match diff --git a/src/tools/utils.ts b/src/tools/utils.ts index 2b4386b..65cb5e2 100644 --- a/src/tools/utils.ts +++ b/src/tools/utils.ts @@ -1,6 +1,7 @@ import { type CachedMetadata, getAllTags, + Notice, parseFrontMatterAliases, Platform, } from 'obsidian' @@ -155,7 +156,13 @@ export function getAltKeyLabel(): 'Alt' | '⌥' { export function isFileImage(path: string): boolean { const ext = getExtension(path) - return ext === 'png' || ext === 'jpg' || ext === 'jpeg' || ext === 'webp' || ext === 'gif' + return ( + ext === 'png' || + ext === 'jpg' || + ext === 'jpeg' || + ext === 'webp' || + ext === 'gif' + ) } export function isFilePDF(path: string): boolean { @@ -252,3 +259,23 @@ function printVerbose(fn: (...args: any[]) => any, ...args: any[]): void { fn(...args) } } + +export const countError = (() => { + let counter = 0 + let alreadyWarned = false + setTimeout(() => { + if (counter > 0) { + --counter + } + }, 1000) + return (immediate = false) => { + // 3 errors in 1 second, there's probably something wrong + if ((++counter >= 5 || immediate) && !alreadyWarned) { + alreadyWarned = true + new Notice( + 'Omnisearch ⚠️ There might be an issue with your cache. You should clean it in Omnisearch settings and restart Obsidian.', + 5000 + ) + } + } +})()