From 59ec915d09586dde25062427381ef1b98df84aa0 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Fri, 21 Feb 2020 12:40:17 -0800 Subject: [PATCH] ensure original error is preserved --- packages/subapp-server/lib/register-routes.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/subapp-server/lib/register-routes.js b/packages/subapp-server/lib/register-routes.js index 74c4a3032..443f522e3 100644 --- a/packages/subapp-server/lib/register-routes.js +++ b/packages/subapp-server/lib/register-routes.js @@ -8,6 +8,7 @@ const HttpStatus = require("./http-status"); const Boom = require("@hapi/boom"); const { ReactWebapp } = require("electrode-react-webapp"); const { resolveChunkSelector, updateFullTemplate } = require("./utils"); +const HttpStatusCodes = require("http-status-codes"); module.exports = function registerRoutes({ routes, topOpts, server }) { // register routes @@ -32,18 +33,22 @@ module.exports = function registerRoutes({ routes, topOpts, server }) { const handler = async (request, h) => { try { const context = await routeHandler({ - content: { html: "", status: 200, useStream: true }, + content: { html: "", status: HttpStatusCodes.OK, useStream: true }, mode: "", request }); const data = context.result; const status = data.status; + if (data instanceof Error) { + throw data; + } + if (status === undefined) { return h .response(data) .type("text/html; charset=UTF-8") - .code(200); + .code(HttpStatusCodes.OK); } else if (HttpStatus.redirect[status]) { return h.redirect(data.path); } else if (HttpStatus.displayHtml[status]) { @@ -56,7 +61,10 @@ module.exports = function registerRoutes({ routes, topOpts, server }) { } catch (err) { if (process.env.NODE_ENV !== "production") { console.error(`Route ${r.name} failed:`, err); - return err.stack; + return h + .response(`
${err.stack}
`) + .type("text/html; charset=UTF-8") + .code(HttpStatusCodes.INTERNAL_SERVER_ERROR); } else { return Boom.internal(); }