Skip to content

Commit 052ef84

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

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class TestHTTPServer {
2121

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

24+
app.use((err, req, res, next) => {
25+
res.set('x-test-error', 'error handler called');
26+
next();
27+
});
28+
2429
return new Promise((resolve, reject) => {
2530
let port = options.port || 3000;
2631
let host = options.host || 'localhost';
@@ -42,9 +47,17 @@ class TestHTTPServer {
4247
});
4348
}
4449

45-
request(urlPath) {
50+
request(urlPath, options) {
4651
let info = this.info;
4752
let url = 'http://[' + info.host + ']:' + info.port;
53+
54+
if (options && options.resolveWithFullResponse) {
55+
return request({
56+
resolveWithFullResponse: options.resolveWithFullResponse,
57+
uri: url + urlPath
58+
});
59+
}
60+
4861
return request(url + urlPath);
4962
}
5063

test/middleware-test.js

Lines changed: 29 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,34 @@ 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);
88+
89+
return server.start()
90+
.then(() => server.request('/', { resolveWithFullResponse: true }))
91+
.then(res => {
92+
expect(res.headers['x-test-error']).to.match(/error handler called/);
93+
});
94+
});
95+
96+
it("propagates to error handling middleware if the resilient flag is not set", function() {
97+
let middleware = fastbootMiddleware({
98+
distPath: fixture('rejected-promise'),
99+
resilient: false,
100+
});
101+
server = new TestHTTPServer(middleware);
102+
103+
return server.start()
104+
.then(() => server.request('/', { resolveWithFullResponse: true }))
105+
.catch(({statusCode, response: { headers} }) => {
106+
expect(headers['x-test-error']).to.match(/error handler called/);
107+
});
108+
});
109+
82110
it("can be provided with a custom FastBoot instance", function() {
83111
let fastboot = new FastBoot({
84112
distPath: fixture('basic-app')

0 commit comments

Comments
 (0)