Add safe error formatter function that prints raw error if it can't be JSON parsed, and improve error output for build.

This commit is contained in:
Dillon Kearns 2022-01-28 11:26:14 -08:00
parent a7f2fa7eda
commit a0462a647e
2 changed files with 20 additions and 9 deletions

View File

@ -2,7 +2,7 @@ const fs = require("./dir-helpers.js");
const fsPromises = require("fs").promises; const fsPromises = require("fs").promises;
const { runElmReview } = require("./compile-elm.js"); const { runElmReview } = require("./compile-elm.js");
const { restoreColor } = require("./error-formatter"); const { restoreColorSafe } = require("./error-formatter");
const path = require("path"); const path = require("path");
const spawnCallback = require("cross-spawn").spawn; const spawnCallback = require("cross-spawn").spawn;
const codegen = require("./codegen.js"); const codegen = require("./codegen.js");
@ -22,7 +22,7 @@ const DIR_PATH = path.join(process.cwd());
const OUTPUT_FILE_NAME = "elm.js"; const OUTPUT_FILE_NAME = "elm.js";
process.on("unhandledRejection", (error) => { process.on("unhandledRejection", (error) => {
console.trace("Unhandled: ", error); console.log(error);
process.exitCode = 1; process.exitCode = 1;
}); });
@ -84,13 +84,12 @@ async function run(options) {
if (isParsingError) { if (isParsingError) {
console.error(error); console.error(error);
} else { } else {
console.error(restoreColor(reviewOutput)); console.error(restoreColorSafe(reviewOutput));
} }
process.exit(1); process.exitCode = 1;
} catch (noElmReviewErrors) { } catch (noElmReviewErrors) {
console.error(error); console.error(error);
} finally { } finally {
process.exit(1);
} }
} }
} }
@ -112,7 +111,7 @@ function initWorker(basePath) {
pagesReady(JSON.parse(message.data)); pagesReady(JSON.parse(message.data));
} else if (message.tag === "error") { } else if (message.tag === "error") {
process.exitCode = 1; process.exitCode = 1;
console.error(restoreColor(message.data)); console.error(restoreColorSafe(message.data));
buildNextPage(newWorker); buildNextPage(newWorker);
} else if (message.tag === "done") { } else if (message.tag === "done") {
buildNextPage(newWorker); buildNextPage(newWorker);
@ -282,9 +281,8 @@ function runElmMake(options, elmEntrypointPath, outputPath, cwd) {
} else { } else {
if (!buildError) { if (!buildError) {
buildError = true; buildError = true;
process.exitCode = 1;
try { try {
reject(restoreColor(JSON.parse(commandOutput))); reject(restoreColorSafe(commandOutput));
} catch (error) { } catch (error) {
reject(commandOutput); reject(commandOutput);
} }

View File

@ -106,6 +106,19 @@ const restoreColor = (error) => {
} }
}; };
/**
* @param {string} error
* @returns {string}
*/
function restoreColorSafe(error) {
try {
const asJson = JSON.parse(error);
return restoreColor(asJson);
} catch (e) {
return error;
}
}
/** /**
* parseMsg :: { errors: Array } -> String * parseMsg :: { errors: Array } -> String
* *
@ -126,7 +139,7 @@ const restoreProblem =
} }
}; };
module.exports = { restoreColor }; module.exports = { restoreColor, restoreColorSafe };
/** @typedef { CompilerError | ReportError | IElmReviewError } RootObject */ /** @typedef { CompilerError | ReportError | IElmReviewError } RootObject */