From 1f85017625bfd414f3b1a0d94ecbc37dc70d712a Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Fri, 27 Jul 2018 13:44:45 +0200 Subject: [PATCH 1/2] Minimize number of errors in LZString parsing errors for history Right now we still see a lot of LZString parsing errors in the logs. They probably come from the user history. We should minimize the number by add the basic length check there as well. Signed-off-by: Sheogorath --- lib/history.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/history.js b/lib/history.js index c7d2472..63be452 100644 --- a/lib/history.js +++ b/lib/history.js @@ -31,6 +31,15 @@ function getHistory (userid, callback) { history = JSON.parse(user.history) // migrate LZString encoded note id to base64url encoded note id for (let i = 0, l = history.length; i < l; i++) { + // Calculate minimal string length for an UUID that is encoded + // base64 encoded and optimize comparsion by using -1 + // this should make a lot of LZ-String parsing errors obsolete + // as we can assume that a nodeId that is 48 chars or longer is a + // noteID. + const base64UuidLength = ((4 * 36) / 3) - 1 + if (!(history[i].id.length > base64UuidLength)) { + continue + } try { let id = LZString.decompressFromBase64(history[i].id) if (id && models.Note.checkNoteIdValid(id)) { From db5b86df4c735a4ed80e1ae683dc58e15f819e0b Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Fri, 27 Jul 2018 13:56:07 +0200 Subject: [PATCH 2/2] Further improvement of error handling for LZString This does some more in depth check on the error message and minimizes the log noise that is caused by LZString. Signed-off-by: Sheogorath --- lib/history.js | 6 +++++- lib/models/note.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/history.js b/lib/history.js index 63be452..9c389bf 100644 --- a/lib/history.js +++ b/lib/history.js @@ -47,7 +47,11 @@ function getHistory (userid, callback) { } } catch (err) { // most error here comes from LZString, ignore - logger.error(err) + if (err.message === 'Cannot read property \'charAt\' of undefined') { + logger.warning('Looks like we can not decode "' + history[i].id + '" with LZString. Can be ignored.') + } else { + logger.error(err) + } } } history = parseHistoryToObject(history) diff --git a/lib/models/note.js b/lib/models/note.js index ec7e2b1..0e8dd4d 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -227,7 +227,11 @@ module.exports = function (sequelize, DataTypes) { var id = LZString.decompressFromBase64(noteId) if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) } } catch (err) { - logger.error(err) + if (err.message === 'Cannot read property \'charAt\' of undefined') { + logger.warning('Looks like we can not decode "' + noteId + '" with LZString. Can be ignored.') + } else { + logger.error(err) + } return _callback(null, null) } },