From 20fdeee04873c47dd5bf28585efff5cbf7cd2cff Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 13 Jan 2020 19:35:06 +0100 Subject: [PATCH] better error handling for search notes --- package-lock.json | 2 +- src/entities/note.js | 4 ++++ src/routes/api/search.js | 26 ++++++++++++++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6fa98a564..e8e6bc4fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "trilium", - "version": "0.39.5", + "version": "0.40.0-beta", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/entities/note.js b/src/entities/note.js index 2dc113032..070d9d4b4 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -110,6 +110,10 @@ class Note extends Entity { async getJsonContent() { const content = await this.getContent(); + if (!content || !content.trim()) { + return null; + } + return JSON.parse(content); } diff --git a/src/routes/api/search.js b/src/routes/api/search.js index c344841c6..b88989fb2 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -29,8 +29,12 @@ async function searchFromNote(req) { return [404, `Note ${req.params.noteId} has not been found.`]; } + if (note.isDeleted) { + return [400, `Note ${req.params.noteId} is deleted.`]; + } + if (note.type !== 'search') { - return [400, '`Note ${req.params.noteId} is not search note.`'] + return [400, `Note ${req.params.noteId} is not search note.`] } const json = await note.getJsonContent(); @@ -41,18 +45,28 @@ async function searchFromNote(req) { let noteIds; - if (json.searchString.startsWith('=')) { - const relationName = json.searchString.substr(1).trim(); + try { + if (json.searchString.startsWith('=')) { + const relationName = json.searchString.substr(1).trim(); - noteIds = await searchFromRelation(note, relationName); + noteIds = await searchFromRelation(note, relationName); + } else { + noteIds = await searchService.searchForNoteIds(json.searchString); + } } - else { - noteIds = await searchService.searchForNoteIds(json.searchString); + catch (e) { + log.error(`Search failed for note ${note.noteId}: ` + e.message + ": " + e.stack); + + throw new Error("Search failed, see logs for details."); } // we won't return search note's own noteId noteIds = noteIds.filter(noteId => noteId !== note.noteId); + if (noteIds.length > 200) { + noteIds = noteIds.slice(0, 200); + } + return noteIds.map(noteCacheService.getNotePath).filter(res => !!res); }