Skip to content

Commit

Permalink
Handle error responses from processes. Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Rusch committed Aug 14, 2018
1 parent 291cc40 commit 00e5912
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@ server

try {
const options = utils.parse(request.query, PENTHOUSE_ALLOWED_OPTIONS);
const cssString = await subprocess(PROCESS_FETCHCSS, {css});

const fetch = await subprocess(PROCESS_FETCHCSS, {css});
if (fetch.error) {
response.status(400).send('Error: Could not fetch css');
return;
}

const cssString = fetch.css;
const critical = await subprocess(PROCESS_PENTHOUSE, {...options, url, cssString});
response.send(critical);
if (critical.error) {
response.status(400).send('Error: Could not process page');
return;
}

response.send(critical.css);
} catch(error) {
console.error(error); // eslint-disable-line no-console
response.status(500).send('Error: ' + error.message);
Expand Down
11 changes: 9 additions & 2 deletions src/process/fetchcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const fetch = require('node-fetch');

process.on('message', (options) => {
fetch(options.css)
.then((res) => res.text())
.then((css) => process.send(css));
.then((response) => {
if (response.ok) {
return response.text();
}

throw new Error(response.statusText);
})
.then((css) => process.send({css}))
.catch((error) => process.send({error}));
});
3 changes: 2 additions & 1 deletion src/process/penthouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ const penthouse = require('penthouse');

process.on('message', (options) => {
penthouse(options)
.then((critical) => process.send(critical));
.then((css) => process.send({css}))
.catch((error) => process.send({error}));
});

0 comments on commit 00e5912

Please sign in to comment.