From 20fdeee04873c47dd5bf28585efff5cbf7cd2cff Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 13 Jan 2020 19:35:06 +0100 Subject: [PATCH 1/3] 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); } From a6cd25071ea6f155fb9acb688e49416e2268efdd Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 18 Jan 2020 08:48:36 +0100 Subject: [PATCH 2/3] more robust handling of sync error, fixes #830 --- .idea/.gitignore | 3 ++- src/services/notes.js | 2 +- src/services/ws.js | 36 +++++++++++++++++++++++++----------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/.idea/.gitignore b/.idea/.gitignore index c0f9e196c..2102c8715 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -2,4 +2,5 @@ /workspace.xml # Datasource local storage ignored files -/dataSources.local.xml \ No newline at end of file +/dataSources.local.xml +/dataSources/ diff --git a/src/services/notes.js b/src/services/notes.js index dacf40d62..2715a4eb5 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -277,7 +277,7 @@ async function saveLinks(note, content) { } const foundLinks = []; -console.log("Scanning", content); + if (note.type === 'text') { content = findImageLinks(content, foundLinks); content = findInternalLinks(content, foundLinks); diff --git a/src/services/ws.js b/src/services/ws.js index 1a1a45831..b4703dfc3 100644 --- a/src/services/ws.js +++ b/src/services/ws.js @@ -71,22 +71,36 @@ function sendMessageToAllClients(message) { } } +async function fillInAdditionalProperties(sync) {throw new Error("AAA"); + // fill in some extra data needed by the frontend + if (sync.entityName === 'attributes') { + sync.noteId = await sql.getValue(`SELECT noteId + FROM attributes + WHERE attributeId = ?`, [sync.entityId]); + } else if (sync.entityName === 'note_revisions') { + sync.noteId = await sql.getValue(`SELECT noteId + FROM note_revisions + WHERE noteRevisionId = ?`, [sync.entityId]); + } else if (sync.entityName === 'branches') { + const {noteId, parentNoteId} = await sql.getRow(`SELECT noteId, parentNoteId + FROM branches + WHERE branchId = ?`, [sync.entityId]); + + sync.noteId = noteId; + sync.parentNoteId = parentNoteId; + } +} + async function sendPing(client) { const syncData = require('./sync_table').getEntitySyncsNewerThan(lastAcceptedSyncIds[client.id]); for (const sync of syncData) { - // fill in some extra data needed by the frontend - if (sync.entityName === 'attributes') { - sync.noteId = await sql.getValue(`SELECT noteId FROM attributes WHERE attributeId = ?`, [sync.entityId]); + try { + await fillInAdditionalProperties(sync); } - else if (sync.entityName === 'note_revisions') { - sync.noteId = await sql.getValue(`SELECT noteId FROM note_revisions WHERE noteRevisionId = ?`, [sync.entityId]); - } - else if (sync.entityName === 'branches') { - const {noteId, parentNoteId} = await sql.getRow(`SELECT noteId, parentNoteId FROM branches WHERE branchId = ?`, [sync.entityId]); - - sync.noteId = noteId; - sync.parentNoteId = parentNoteId; + catch (e) { + log.error("Could not fill additional properties for sync " + JSON.stringify(sync) + + " because of error: " + e.message + ": " + e.stack); } } From 0ec11d29ba2d524ff80b49ab8dc40f3f8cf3835b Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 18 Jan 2020 08:59:46 +0100 Subject: [PATCH 3/3] fix creating root calendar note when missing, #752 --- src/services/date_notes.js | 4 +++- src/services/ws.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/services/date_notes.js b/src/services/date_notes.js index d6b1a396b..c71ccb946 100644 --- a/src/services/date_notes.js +++ b/src/services/date_notes.js @@ -40,7 +40,9 @@ async function getRootCalendarNote() { parentNoteId: 'root', title: 'Calendar', target: 'into', - isProtected: false + isProtected: false, + type: 'text', + content: '' })).note; await attributeService.createLabel(rootNote.noteId, CALENDAR_ROOT_LABEL); diff --git a/src/services/ws.js b/src/services/ws.js index b4703dfc3..5eae3f0f7 100644 --- a/src/services/ws.js +++ b/src/services/ws.js @@ -71,7 +71,7 @@ function sendMessageToAllClients(message) { } } -async function fillInAdditionalProperties(sync) {throw new Error("AAA"); +async function fillInAdditionalProperties(sync) { // fill in some extra data needed by the frontend if (sync.entityName === 'attributes') { sync.noteId = await sql.getValue(`SELECT noteId