Catch errors in search worker

This commit is contained in:
squidfunk 2019-12-20 10:24:06 +01:00
parent 2fa347aaae
commit b31061dbeb

View File

@ -181,21 +181,13 @@ export class Search {
* page. For this reason, section results are grouped within their respective
* articles which are the top-level results that are returned.
*
* Rogue control characters must be filtered before handing the query to the
* search index, as lunr will throw otherwise.
*
* @param query - Query string
*
* @return Search results
*/
public search(query: string): SearchResult[] {
query = query
.replace(/(?:^|\s+)[*+-:^~]+(?=\s+|$)/g, "")
.trim()
/* Abort early, if query is empty */
if (!query)
return []
if (query) {
try {
/* Group sections by containing article */
const groups = this.index.search(query)
@ -223,6 +215,16 @@ export class Search {
return fn(this.documents.get(section.ref) as SectionDocument)
})
}))
/* Log errors to console (for now) */
} catch (err) {
// tslint:disable-next-line no-console
console.warn(`Invalid query: ${query} see https://bit.ly/2s3ChXG`)
}
}
/* Return nothing in case of error or empty query */
return []
}
/**