diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index ea7d84e4b..e3f01c57d 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -185,8 +185,10 @@ async function setupRoutesFromFile(srcDir, server, pluginOpts) { const data = context.result; const status = data.status; - - if (status === undefined) { + if (data instanceof Error) { + // rethrow to get default error behavior below with helpful errors in dev mode + throw data; + } else if (status === undefined) { return h.response(data).type("text/html; charset=UTF-8").code(200); } else if (HttpStatus.redirect[status]) { return h.redirect(data.path).code(status); diff --git a/packages/subapp-server/test/spec/setup-hapi-routes.spec.js b/packages/subapp-server/test/spec/setup-hapi-routes.spec.js index 8cca81eb4..12de2f0ec 100644 --- a/packages/subapp-server/test/spec/setup-hapi-routes.spec.js +++ b/packages/subapp-server/test/spec/setup-hapi-routes.spec.js @@ -258,6 +258,24 @@ describe("setupSubAppHapiRoutes", () => { delete process.env.NODE_ENV; }); + it("should let the server reply error stack if routeHandler returns an error as a result", async () => { + stubPathResolve = getStubResolve1(); + stubRouteHandler = sinon.stub(ReactWebapp, "makeRouteHandler").callsFake(() => async () => ({ + result: new Error("Dev error here") + })); + const logs = []; + const stubConsoleError = sinon.stub(console, "error").callsFake(c => logs.push(c)); + await setupSubAppHapiRoutes(server, {}); + await server.start(); + const { result: devResult } = await server.inject({ + method: "GET", + url: "/file2" + }); + expect(devResult).to.include("Dev error here"); + expect(logs[0]).to.equal("Route file2 failed:"); + stubConsoleError.restore(); + }); + it("should throw error if route.htmlFile not defined", async () => { stubPathResolve = getStubResolve3(); try {