Skip to content

Commit

Permalink
add response callback for error and bad status
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 3, 2018
1 parent 82495cb commit 54652fb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
9 changes: 6 additions & 3 deletions packages/electrode-react-webapp/lib/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require("lodash");
const assert = require("assert");
const ReactWebapp = require("../react-webapp");
const HttpStatus = require("../http-status");
const { htmlifyError } = require("../utils");
const { responseForError, responseForBadStatus } = require("../utils");

const DefaultHandleRoute = (request, response, handler, content, routeOptions) => {
return handler({ content, mode: request.query.__mode || "", request })
Expand All @@ -28,11 +28,13 @@ const DefaultHandleRoute = (request, response, handler, content, routeOptions) =
} else if (status >= 200 && status < 300) {
response.send(data.html !== undefined ? data.html : data);
} else {
response.status(status).send(data);
const output = routeOptions.responseForBadStatus(request, routeOptions, data);
response.status(output.status).send(output.html);
}
})
.catch(err => {
response.status(err.status || 500).send(htmlifyError(err, routeOptions.replyErrorStack));
const output = routeOptions.responseForError(request, routeOptions, err);
response.status(output.status).send(output.html);
});
};

Expand All @@ -56,6 +58,7 @@ const registerRoutes = (app, options, next = () => {}) => {
const routeOptions = _.defaults({ htmlFile: v.htmlFile }, registerOptions);
const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const handleRoute = options.handleRoute || DefaultHandleRoute;
_.defaults(routeOptions, { responseForError, responseForBadStatus });
let content;

/*eslint max-nested-callbacks: [0, 4]*/
Expand Down
13 changes: 10 additions & 3 deletions packages/electrode-react-webapp/lib/hapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _ = require("lodash");
const assert = require("assert");
const ReactWebapp = require("../react-webapp");
const HttpStatus = require("../http-status");
const { htmlifyError } = require("../utils");
const { responseForError, responseForBadStatus } = require("../utils");

const DefaultHandleRoute = (request, reply, handler, content, routeOptions) => {
return handler({
Expand Down Expand Up @@ -34,7 +34,9 @@ const DefaultHandleRoute = (request, reply, handler, content, routeOptions) => {
} else if (HttpStatus.displayHtml[status] || (status >= 200 && status < 300)) {
respond = reply(data.html !== undefined ? data.html : data);
} else {
respond = reply(data);
const output = routeOptions.responseForBadStatus(request, routeOptions, data);
status = output.status;
respond = reply(output.html);
}

const response = context.user && context.user.response;
Expand All @@ -46,12 +48,15 @@ const DefaultHandleRoute = (request, reply, handler, content, routeOptions) => {
return respond.code(status);
})
.catch(err => {
reply(htmlifyError(err, routeOptions.replyErrorStack)).code(err.status || 500);
const output = routeOptions.responseForError(request, routeOptions, err);

reply(output.html).code(output.status);
});
};

const registerRoutes = (server, options) => {
const registerOptions = ReactWebapp.setupOptions(options);

_.each(registerOptions.paths, (pathData, path) => {
const resolveContent = () => {
if (registerOptions.serverSideRendering !== false) {
Expand All @@ -61,6 +66,7 @@ const registerRoutes = (server, options) => {
);
return ReactWebapp.resolveContent(pathData.content);
}

return {
content: {
status: 200,
Expand All @@ -72,6 +78,7 @@ const registerRoutes = (server, options) => {
const routeOptions = ReactWebapp.setupPathOptions(registerOptions, path);
const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const handleRoute = options.handleRoute || DefaultHandleRoute;
_.defaults(routeOptions, { responseForError, responseForBadStatus });
let content;

server.route({
Expand Down
9 changes: 6 additions & 3 deletions packages/electrode-react-webapp/lib/koa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _ = require("lodash");
const assert = require("assert");
const ReactWebapp = require("../react-webapp");
const HttpStatus = require("../http-status");
const { htmlifyError } = require("../utils");
const { responseForError, responseForBadStatus } = require("../utils");

function DefaultHandleRoute(handler, content, routeOptions) {
const request = this.request;
Expand Down Expand Up @@ -35,11 +35,13 @@ function DefaultHandleRoute(handler, content, routeOptions) {
} else if (status >= 200 && status < 300) {
respond(status, data.html !== undefined ? data.html : data);
} else {
respond(status, data);
const output = routeOptions.responseForBadStatus(request, routeOptions, data);
respond(output.status, output.html);
}
})
.catch(err => {
respond(err.status || 500, htmlifyError(err, routeOptions.replyErrorStack));
const output = routeOptions.responseForError(request, routeOptions, err);
respond(output.status, output.html);
});
}

Expand All @@ -62,6 +64,7 @@ const registerRoutes = (router, options) => {
const routeOptions = _.defaults({ htmlFile: v.htmlFile }, registerOptions);
const routeHandler = ReactWebapp.makeRouteHandler(routeOptions);
const handleRoute = options.handleRoute || DefaultHandleRoute;
_.defaults(routeOptions, { responseForError, responseForBadStatus });
let content;

let methods = v.method || ["GET"];
Expand Down
18 changes: 17 additions & 1 deletion packages/electrode-react-webapp/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ function getCspNonce(request, cspNonceValue) {

const resolvePath = path => (!Path.isAbsolute(path) ? Path.resolve(path) : path);

function responseForError(request, routeOptions, err) {
return {
status: err.status || 500,
html: htmlifyError(err, routeOptions.replyErrorStack)
};
}

function responseForBadStatus(request, routeOptions, data) {
return {
status: data.status,
html: data
};
}

module.exports = {
resolveChunkSelector,
loadAssetsFromStats,
Expand All @@ -199,5 +213,7 @@ module.exports = {
getDevJsBundle,
getProdBundles,
processRenderSsMode,
getCspNonce
getCspNonce,
responseForError,
responseForBadStatus
};

0 comments on commit 54652fb

Please sign in to comment.