From 5280b6ed637e7f8be7fe86de6f33f8155cbb4d2c Mon Sep 17 00:00:00 2001 From: AWolf81 Date: Thu, 24 Oct 2019 20:58:47 +0200 Subject: [PATCH] Add error handling --- browser/main/lib/dataApi/createNoteFromUrl.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/browser/main/lib/dataApi/createNoteFromUrl.js b/browser/main/lib/dataApi/createNoteFromUrl.js index f9878210..e1817744 100644 --- a/browser/main/lib/dataApi/createNoteFromUrl.js +++ b/browser/main/lib/dataApi/createNoteFromUrl.js @@ -14,12 +14,18 @@ function validateUrl (str) { } } +const ERROR_MESSAGES = { + ENOTFOUND: 'URL not found. Please check the URL, your internet connection and try again.', + VALIDATION_ERROR: 'Please check your URL is in correct format. (Example, https://www.google.com)', + UNEXPECTED: 'Unexpected error! Please check console for details!' +} + function createNoteFromUrl (url, storage, folder, dispatch = null, location = null) { return new Promise((resolve, reject) => { const td = createTurndownService() if (!validateUrl(url)) { - reject({result: false, error: 'Please check your URL is in correct format. (Example, https://www.google.com)'}) + reject({result: false, error: ERROR_MESSAGES.VALIDATION_ERROR}) } const request = url.startsWith('https') ? https : http @@ -70,8 +76,9 @@ function createNoteFromUrl (url, storage, folder, dispatch = null, location = nu req.on('error', (e) => { console.error('error in parsing URL', e) - reject({result: false, error: e}) + reject({result: false, error: ERROR_MESSAGES[e.code] || ERROR_MESSAGES.UNEXPECTED}) }) + req.end() }) }