import: Improve error logging

This commit is contained in:
Richard Hansen 2021-02-18 03:42:41 -05:00
parent 22a67c5dc6
commit fb745374c3

View file

@ -103,20 +103,16 @@ const doImport = async (req, res, padId) => {
// locally wrapped Promise, since form.parse requires a callback // locally wrapped Promise, since form.parse requires a callback
let srcFile = await new Promise((resolve, reject) => { let srcFile = await new Promise((resolve, reject) => {
form.parse(req, (err, fields, files) => { form.parse(req, (err, fields, files) => {
if (err || files.file === undefined) { if (err != null) {
// the upload failed, stop at this point logger.warn(`Import failed due to form error: ${err.stack || err}`);
if (err) {
console.warn(`Uploading Error: ${err.stack}`);
}
// I hate doing indexOf here but I can't see anything to use... // I hate doing indexOf here but I can't see anything to use...
if (err && err.stack && err.stack.indexOf('maxFileSize') !== -1) { if (err && err.stack && err.stack.indexOf('maxFileSize') !== -1) {
return reject(new ImportError('maxFileSize')); return reject(new ImportError('maxFileSize'));
} }
return reject(new ImportError('uploadFailed')); return reject(new ImportError('uploadFailed'));
} }
if (!files.file) { // might not be a graceful fix but it works if (!files.file) {
logger.warn('Import failed because form had no file');
return reject(new ImportError('uploadFailed')); return reject(new ImportError('uploadFailed'));
} }
resolve(files.file.path); resolve(files.file.path);
@ -140,7 +136,7 @@ const doImport = async (req, res, padId) => {
srcFile = path.join(path.dirname(srcFile), `${path.basename(srcFile, fileEnding)}.txt`); srcFile = path.join(path.dirname(srcFile), `${path.basename(srcFile, fileEnding)}.txt`);
await fs.rename(oldSrcFile, srcFile); await fs.rename(oldSrcFile, srcFile);
} else { } else {
console.warn('Not allowing unknown file type to be imported', fileEnding); logger.warn(`Not allowing unknown file type to be imported: ${fileEnding}`);
throw new ImportError('uploadFailed'); throw new ImportError('uploadFailed');
} }
} }
@ -188,7 +184,7 @@ const doImport = async (req, res, padId) => {
convertor.convertFile(srcFile, destFile, exportExtension, (err) => { convertor.convertFile(srcFile, destFile, exportExtension, (err) => {
// catch convert errors // catch convert errors
if (err) { if (err) {
console.warn('Converting Error:', err); logger.warn(`Converting Error: ${err.stack || err}`);
return reject(new ImportError('convertFailed')); return reject(new ImportError('convertFailed'));
} }
resolve(); resolve();
@ -205,6 +201,7 @@ const doImport = async (req, res, padId) => {
const isAscii = !Array.prototype.some.call(buf, (c) => (c > 240)); const isAscii = !Array.prototype.some.call(buf, (c) => (c > 240));
if (!isAscii) { if (!isAscii) {
logger.warn('Attempt to import non-ASCII file');
throw new ImportError('uploadFailed'); throw new ImportError('uploadFailed');
} }
} }
@ -230,8 +227,8 @@ const doImport = async (req, res, padId) => {
if (importHandledByPlugin || useConvertor || fileIsHTML) { if (importHandledByPlugin || useConvertor || fileIsHTML) {
try { try {
await importHtml.setPadHTML(pad, text); await importHtml.setPadHTML(pad, text);
} catch (e) { } catch (err) {
logger.warn('Error importing, possibly caused by malformed HTML'); logger.warn(`Error importing, possibly caused by malformed HTML: ${err.stack || err}`);
} }
} else { } else {
await pad.setText(text); await pad.setText(text);