Catching possible errors

This commit is contained in:
Simon Cambier
2022-10-22 19:59:10 +02:00
parent 77d583d5f8
commit ecbbcd4e99
2 changed files with 54 additions and 44 deletions

View File

@@ -54,25 +54,6 @@ class CacheManager {
return !indexedNote || indexedNote.mtime !== file.stat.mtime return !indexedNote || indexedNote.mtime !== file.stat.mtime
} }
// private async _writeMinisearchIndex(minisearch: MiniSearch): Promise<void> {
// if (!settings.persistCache) {
// return
// }
// const json = JSON.stringify(minisearch)
// const data = deflate(json)
// await app.vault.adapter.writeBinary(minisearchCacheFilePath, data as any)
// console.log('Omnisearch - Minisearch index saved on disk')
// }
//
// private async _saveNotesCache() {
// if (!settings.persistCache) {
// return
// }
// const json = JSON.stringify(Array.from(this.documentsCache.entries()))
// const data = deflate(json)
// await app.vault.adapter.writeBinary(notesCacheFilePath, data as any)
// console.log('Omnisearch - Notes cache saved on disk')
// }
} }
export const cacheManager = new CacheManager() export const cacheManager = new CacheManager()

View File

@@ -1,4 +1,4 @@
import { type CachedMetadata, Platform } from 'obsidian' import { type CachedMetadata, Notice, Platform } from 'obsidian'
import type { SearchMatch } from '../globals' import type { SearchMatch } from '../globals'
import { import {
excerptAfter, excerptAfter,
@@ -78,6 +78,7 @@ export function loopIndex(index: number, nbItems: number): number {
} }
export function makeExcerpt(content: string, offset: number): string { export function makeExcerpt(content: string, offset: number): string {
try {
const pos = offset ?? -1 const pos = offset ?? -1
if (pos > -1) { if (pos > -1) {
const from = Math.max(0, pos - excerptBefore) const from = Math.max(0, pos - excerptBefore)
@@ -88,6 +89,14 @@ export function makeExcerpt(content: string, offset: number): string {
(to < content.length - 1 ? '…' : '') (to < content.length - 1 ? '…' : '')
} }
return escapeHTML(content) return escapeHTML(content)
} catch (e) {
new Notice(
'Omnisearch - Error while creating excerpt, see developer console'
)
console.error(`Omnisearch - Error while creating excerpt`)
console.error(e)
return ''
}
} }
/** /**
@@ -142,14 +151,26 @@ export function getAliasesFromMetadata(
metadata: CachedMetadata | null metadata: CachedMetadata | null
): string[] { ): string[] {
const arrOrString = metadata?.frontmatter?.aliases ?? [] const arrOrString = metadata?.frontmatter?.aliases ?? []
try {
return ( return (
Array.isArray(arrOrString) ? arrOrString : arrOrString.toString().split(',') Array.isArray(arrOrString)
? arrOrString
: arrOrString.toString().split(',')
) )
.map(s => (s ? s.trim() : s)) .map(s => (s ? s.trim() : s))
.filter(s => !!s) .filter(s => !!s)
} catch (e) {
new Notice(
'Omnisearch - Error while getting aliases from file, see developer console'
)
console.error(`Omnisearch - Error while getting aliases from file`)
console.error(e)
return []
}
} }
export function getTagsFromMetadata(metadata: CachedMetadata | null): string[] { export function getTagsFromMetadata(metadata: CachedMetadata | null): string[] {
try {
const arrOrString = metadata?.frontmatter?.tags ?? [] const arrOrString = metadata?.frontmatter?.tags ?? []
const fromFrontMatter = ( const fromFrontMatter = (
Array.isArray(arrOrString) ? arrOrString : arrOrString.split(',') Array.isArray(arrOrString) ? arrOrString : arrOrString.split(',')
@@ -161,6 +182,14 @@ export function getTagsFromMetadata(metadata: CachedMetadata | null): string[] {
return [...fromFrontMatter, ...fromBody].map(t => return [...fromFrontMatter, ...fromBody].map(t =>
t[0] !== '#' ? '#' + t : t t[0] !== '#' ? '#' + t : t
) )
} catch (e) {
new Notice(
'Omnisearch - Error while getting tags from file, see developer console'
)
console.error(`Omnisearch - Error while getting tags from file`)
console.error(e)
return []
}
} }
/** /**