Skip to content

Commit b975bb3

Browse files
author
Arjan Singh
committed
Forward errors along to error handling middlewares
1 parent 3be164a commit b975bb3

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@ function fastbootExpressMiddleware(distPath, options) {
3636
result.html()
3737
.then(html => {
3838
let headers = result.headers;
39+
let statusMessage = result.error ? 'NOT OK ' : 'OK ';
3940

4041
for (var pair of headers.entries()) {
4142
res.set(pair[0], pair[1]);
4243
}
4344

44-
log(result.statusCode, 'OK ' + path);
45+
if (result.error) {
46+
log("RESILIENT MODE CAUGHT:", result.error.stack);
47+
next(result.error);
48+
}
49+
50+
log(result.statusCode, statusMessage + path);
4551
res.status(result.statusCode);
4652
res.send(html);
4753
})
4854
.catch(error => {
49-
console.log(error.stack);
55+
log(500, error.stack);
56+
next(error);
5057
res.sendStatus(500);
5158
});
5259
}
@@ -55,6 +62,7 @@ function fastbootExpressMiddleware(distPath, options) {
5562
if (error.name === "UnrecognizedURLError") {
5663
next();
5764
} else {
65+
next(error);
5866
log(500, "Unknown Error: " + error.stack);
5967
if (error.stack) {
6068
res.status(500).send(error.stack);

test/helpers/test-http-server.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class TestHTTPServer {
2121

2222
app.get('/*', this.middleware);
2323

24+
if (options.errorHandling) {
25+
app.use((err, req, res, next) => {
26+
res.set('x-test-error', 'error handler called');
27+
next();
28+
});
29+
}
30+
2431
return new Promise((resolve, reject) => {
2532
let port = options.port || 3000;
2633
let host = options.host || 'localhost';
@@ -42,9 +49,17 @@ class TestHTTPServer {
4249
});
4350
}
4451

45-
request(urlPath) {
52+
request(urlPath, options) {
4653
let info = this.info;
4754
let url = 'http://[' + info.host + ']:' + info.port;
55+
56+
if (options && options.resolveWithFullResponse) {
57+
return request({
58+
resolveWithFullResponse: options.resolveWithFullResponse,
59+
uri: url + urlPath
60+
});
61+
}
62+
4863
return request(url + urlPath);
4964
}
5065

test/middleware-test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("FastBoot", function() {
6565
});
6666
});
6767

68-
it("renders an empty page if the resilient flag is set", function() {
68+
it("renders no FastBoot markup if the resilient flag is set", function() {
6969
let middleware = fastbootMiddleware({
7070
distPath: fixture('rejected-promise'),
7171
resilient: true
@@ -79,6 +79,54 @@ describe("FastBoot", function() {
7979
});
8080
});
8181

82+
it("propagates to error handling middleware if the resilient flag is set", function() {
83+
let middleware = fastbootMiddleware({
84+
distPath: fixture('rejected-promise'),
85+
resilient: true
86+
});
87+
server = new TestHTTPServer(middleware, { errorHandling: true });
88+
89+
return server.start()
90+
.then(() => server.request('/', { resolveWithFullResponse: true }))
91+
.then(({ body, statusCode, headers }) => {
92+
expect(statusCode).to.equal(200);
93+
expect(headers['x-test-error']).to.match(/error handler called/);
94+
expect(body).to.match(/hello world/);
95+
});
96+
});
97+
98+
it("propagates to error handling middleware if the resilient flag is not set", function() {
99+
let middleware = fastbootMiddleware({
100+
distPath: fixture('rejected-promise'),
101+
resilient: false,
102+
});
103+
server = new TestHTTPServer(middleware, { errorHandling: true });
104+
105+
return server.start()
106+
.then(() => server.request('/', { resolveWithFullResponse: true }))
107+
.catch(({ statusCode, response: { headers } }) => {
108+
expect(statusCode).to.equal(500);
109+
expect(headers['x-test-error']).to.match(/error handler called/);
110+
});
111+
});
112+
113+
it("is does not propagate errors when the reslient flag is set and there is no error handling middleware", function() {
114+
let middleware = fastbootMiddleware({
115+
distPath: fixture('rejected-promise'),
116+
resilient: true,
117+
});
118+
server = new TestHTTPServer(middleware, { errorHandling: false });
119+
120+
return server.start()
121+
.then(() => server.request('/', { resolveWithFullResponse: true }))
122+
.then(({ body, statusCode, headers }) => {
123+
expect(statusCode).to.equal(200);
124+
expect(headers['x-test-error']).to.not.match(/error handler called/);
125+
expect(body).to.not.match(/error/);
126+
expect(body).to.match(/hello world/);
127+
});
128+
});
129+
82130
it("can be provided with a custom FastBoot instance", function() {
83131
let fastboot = new FastBoot({
84132
distPath: fixture('basic-app')

0 commit comments

Comments
 (0)