Clean up serverless render adapter code and return HTML error code.

This commit is contained in:
Dillon Kearns 2021-03-28 19:30:38 -07:00
parent fd975ea45d
commit 0c564bc5fe
2 changed files with 18 additions and 22 deletions

View File

@ -6,17 +6,23 @@ exports.handler =
* @param {any} context
*/
async function (event, context) {
// event.path
console.log(JSON.stringify(event));
// process.chdir(path.join(__dirname, "../../"));
// process.chdir("../");
// const compiledElmPath = path.join(process.cwd(), "elm-pages-cli.js");
const compiledElmPath = path.join(__dirname, "elm-pages-cli.js");
const renderer = require("../../../../generator/src/render");
// console.log("pwd", process.cwd());
return {
body: (await renderer(compiledElmPath, event.path, event)).htmlString,
statusCode: 200,
};
try {
const renderResult = await renderer(compiledElmPath, event.path, event);
return {
body: renderResult.htmlString,
statusCode: 200,
};
} catch (error) {
return {
// body: JSON.stringify({ error }),
body: `<body><h1>Error</h1><pre>${error}</pre></body>`,
statusCode: 500,
headers: [{ "content-type": "text/html" }],
};
}
};

View File

@ -7,8 +7,7 @@ const seo = require("./seo-renderer.js");
let foundErrors = false;
process.on("unhandledRejection", (error) => {
console.error(error);
process.exit(1);
console.error("@@@ UNHANDLED", error);
});
module.exports =
@ -21,7 +20,6 @@ module.exports =
*/
async function run(compiledElmPath, path, request) {
XMLHttpRequest = require("xhr2");
console.log("RENDER NEW");
const result = await runElmApp(compiledElmPath, path, request);
return result;
};
@ -32,15 +30,7 @@ module.exports =
* @param {import('aws-lambda').APIGatewayProxyEvent} request
*/
function runElmApp(compiledElmPath, path, request) {
process.on("beforeExit", (code) => {
if (foundErrors) {
process.exit(1);
} else {
process.exit(0);
}
});
return new Promise((resolve, _) => {
return new Promise((resolve, reject) => {
const mode /** @type { "dev" | "prod" } */ = "elm-to-html-beta";
const staticHttpCache = {};
const app = require(compiledElmPath).Elm.Main.init({
@ -56,8 +46,8 @@ function runElmApp(compiledElmPath, path, request) {
resolve(outputString(fromElm));
}
} else if (fromElm.tag === "Errors") {
console.error(fromElm.args[0]);
foundErrors = true;
reject(fromElm.args[0]);
} else {
console.log(fromElm);
}