From 00e5912c7953d82d7be2ebd0121e83562472e672 Mon Sep 17 00:00:00 2001 From: Norman Rusch Date: Tue, 14 Aug 2018 16:40:22 +0200 Subject: [PATCH] Handle error responses from processes. Fixes #1 --- src/index.js | 16 ++++++++++++++-- src/process/fetchcss.js | 11 +++++++++-- src/process/penthouse.js | 3 ++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 7e699a5..7ae75a6 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/src/process/fetchcss.js b/src/process/fetchcss.js index 6b6558e..e1ff45a 100644 --- a/src/process/fetchcss.js +++ b/src/process/fetchcss.js @@ -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})); }); diff --git a/src/process/penthouse.js b/src/process/penthouse.js index 9f7947c..45e6cd3 100644 --- a/src/process/penthouse.js +++ b/src/process/penthouse.js @@ -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})); });