Skip to content

Commit

Permalink
Serve 500s when the bundler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Dec 7, 2017
1 parent c3df809 commit 36b2054
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ function middleware(bundler) {
}

function respond() {
// If the URL doesn't start with the public path, send the main HTML bundle
if (!req.url.startsWith(bundler.options.publicURL)) {
if (bundler.errored) {
return send500();
} else if (!req.url.startsWith(bundler.options.publicURL)) {
// If the URL doesn't start with the public path, send the main HTML bundle
return sendIndex();
} else {
// Otherwise, serve the file from the dist folder
Expand All @@ -35,6 +37,11 @@ function middleware(bundler) {
}
}

function send500() {
res.writeHead(500);
res.end('🚨 Build error, check the console for details.');
}

function send404() {
if (next) {
return next();
Expand Down
17 changes: 17 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ describe('server', function () {

assert(threw);
});

it('should serve a 500 if the bundler errored', async function () {
let b = bundler(__dirname + '/integration/html/index.html');
server = b.serve(0);

b.errored = true;

try {
await get('/');
throw new Error('GET / responded with 200')
} catch (err) {
assert.equal(err.message, 'Request failed: 500');
}

b.errored = false;
await get('/');
});
});

0 comments on commit 36b2054

Please sign in to comment.