1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Desktop, Cli: Improved error handling when importing ENEX

This commit is contained in:
Laurent Cozic 2021-01-24 15:24:10 +00:00
parent 039da8c15c
commit 257cde4383

View File

@ -264,15 +264,17 @@ async function saveNoteToStorage(note, importOptions) {
function importEnex(parentFolderId, filePath, importOptions = null) { function importEnex(parentFolderId, filePath, importOptions = null) {
if (!importOptions) importOptions = {}; if (!importOptions) importOptions = {};
// console.info(JSON.stringify({importOptions}, null, 2));
if (!('fuzzyMatching' in importOptions)) importOptions.fuzzyMatching = false; if (!('fuzzyMatching' in importOptions)) importOptions.fuzzyMatching = false;
if (!('onProgress' in importOptions)) importOptions.onProgress = function() {}; if (!('onProgress' in importOptions)) importOptions.onProgress = function() {};
if (!('onError' in importOptions)) importOptions.onError = function() {}; if (!('onError' in importOptions)) importOptions.onError = function() {};
const handleSaxStreamEvent = (fn) => { function handleSaxStreamEvent(fn) {
return function(...args) { return function(...args) {
// Pass the parser to the wrapped function for debugging purposes
if (this._parser) fn._parser = this._parser;
try { try {
fn(...args); fn.call(this, ...args);
} catch (error) { } catch (error) {
if (importOptions.onError) { if (importOptions.onError) {
importOptions.onError(error); importOptions.onError(error);
@ -281,9 +283,9 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
} }
} }
}; };
}; }
return new Promise((resolve, reject) => { return new Promise((resolve) => {
const progressState = { const progressState = {
loaded: 0, loaded: 0,
created: 0, created: 0,
@ -308,8 +310,27 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
const notes = []; const notes = [];
let processingNotes = false; let processingNotes = false;
stream.on('error', error => { const createErrorWithNoteTitle = (fnThis, error) => {
reject(new Error(error.toString())); const line = [];
const parser = fnThis ? fnThis._parser : null;
if (parser) {
line.push(`Line ${parser.line}:${parser.column}`);
}
if (note && note.title) {
line.push(`"${note.title}"`);
}
line.push(error.message);
error.message = line.join(': ');
return error;
};
stream.on('error', function(error) {
importOptions.onError(createErrorWithNoteTitle(this, error));
}); });
function currentNodeName() { function currentNodeName() {
@ -338,7 +359,7 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
try { try {
resource = await processNoteResource(resource); resource = await processNoteResource(resource);
} catch (error) { } catch (error) {
importOptions.onError(error); importOptions.onError(createErrorWithNoteTitle(null, error));
continue; continue;
} }
@ -387,7 +408,7 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
importOptions.onProgress(progressState); importOptions.onProgress(progressState);
} catch (error) { } catch (error) {
const newError = wrapError(`Error on note "${note.title}"`, error); const newError = wrapError(`Error on note "${note.title}"`, error);
importOptions.onError(newError); importOptions.onError(createErrorWithNoteTitle(null, newError));
} }
} }
@ -396,8 +417,8 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
return true; return true;
} }
saxStream.on('error', error => { saxStream.on('error', function(error) {
importOptions.onError(error); importOptions.onError(createErrorWithNoteTitle(this, error));
}); });
saxStream.on('text', handleSaxStreamEvent(function(text) { saxStream.on('text', handleSaxStreamEvent(function(text) {
@ -439,7 +460,7 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
} else if (n == 'content') { } else if (n == 'content') {
// Ignore - white space between the opening tag <content> and the <![CDATA[< block where the content actually is // Ignore - white space between the opening tag <content> and the <![CDATA[< block where the content actually is
} else { } else {
console.warn(`Unsupported note tag: ${n}`); console.warn(createErrorWithNoteTitle(this, new Error(`Unsupported note tag: ${n}`)));
} }
} }
})); }));
@ -492,7 +513,7 @@ function importEnex(parentFolderId, filePath, importOptions = null) {
if (notes.length >= 10) { if (notes.length >= 10) {
processNotes().catch(error => { processNotes().catch(error => {
importOptions.onError(error); importOptions.onError(createErrorWithNoteTitle(this, error));
}); });
} }
note = null; note = null;