#467 - Higher tolerance for errors during indexing, show error when result is missing from search

This commit is contained in:
Simon Cambier
2025-06-06 19:17:44 +02:00
parent 9f13544bb0
commit d66aeeafef
3 changed files with 48 additions and 17 deletions

View File

@@ -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
)
}
}
}

View File

@@ -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

View File

@@ -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
)
}
}
})()