diff --git a/src/public/javascripts/dialogs/import.js b/src/public/javascripts/dialogs/import.js index da2dc79f0..f482d104e 100644 --- a/src/public/javascripts/dialogs/import.js +++ b/src/public/javascripts/dialogs/import.js @@ -63,6 +63,12 @@ function importIntoNote(importNoteId) { } messagingService.subscribeToMessages(async message => { + if (message.type === 'import-error') { + infoService.showError(message.message); + $dialog.modal('hide'); + return; + } + if (!message.importId || message.importId !== importId) { // incoming messages must correspond to this import instance return; diff --git a/src/routes/api/import.js b/src/routes/api/import.js index 556bb35ec..1b50c4a8c 100644 --- a/src/routes/api/import.js +++ b/src/routes/api/import.js @@ -9,6 +9,7 @@ const messagingService = require('../../services/messaging'); const cls = require('../../services/cls'); const path = require('path'); const noteCacheService = require('../../services/note_cache'); +const log = require('../../services/log'); class ImportContext { constructor(importId) { @@ -40,6 +41,14 @@ class ImportContext { noteId: noteId }); } + + // must remaing static + async reportError(message) { + await messagingService.sendMessageToAllClients({ + type: 'import-error', + message: message + }); + } } async function importToBranch(req) { @@ -66,23 +75,28 @@ async function importToBranch(req) { const importContext = new ImportContext(importId); - if (extension === '.tar') { - note = await tarImportService.importTar(importContext, file.buffer, parentNote); + try { + if (extension === '.tar') { + note = await tarImportService.importTar(importContext, file.buffer, parentNote); + } else if (extension === '.opml') { + note = await opmlImportService.importOpml(importContext, file.buffer, parentNote); + } else if (extension === '.md') { + note = await singleImportService.importMarkdown(importContext, file, parentNote); + } else if (extension === '.html' || extension === '.htm') { + note = await singleImportService.importHtml(importContext, file, parentNote); + } else if (extension === '.enex') { + note = await enexImportService.importEnex(importContext, file, parentNote); + } else { + return [400, `Unrecognized extension ${extension}, must be .tar or .opml`]; + } } - else if (extension === '.opml') { - note = await opmlImportService.importOpml(importContext, file.buffer, parentNote); - } - else if (extension === '.md') { - note = await singleImportService.importMarkdown(importContext, file, parentNote); - } - else if (extension === '.html' || extension === '.htm') { - note = await singleImportService.importHtml(importContext, file, parentNote); - } - else if (extension === '.enex') { - note = await enexImportService.importEnex(importContext, file, parentNote); - } - else { - return [400, `Unrecognized extension ${extension}, must be .tar or .opml`]; + catch (e) { + const message = "Import failed with following error: '" + e.message + "'. More details might be in the logs."; + importContext.reportError(message); + + log.error(message + e.stack); + + return [500, message]; } // import has deactivated note events so note cache is not updated diff --git a/src/services/import/tar.js b/src/services/import/tar.js index cf0b44a08..eb18f3c7c 100644 --- a/src/services/import/tar.js +++ b/src/services/import/tar.js @@ -24,7 +24,7 @@ let lastSentCountTs = Date.now(); */ async function importTar(importContext, fileBuffer, importRootNote) { importNoteCount = 0; - + throw new Error("Hello"); // maps from original noteId (in tar file) to newly generated noteId const noteIdMap = {}; const attributes = [];