@@ 120,7 120,19 @@ const convert = (dom, title) => {
const args = process.argv.slice(2);
fetch(args[0])
- .then(res => res.text())
+ .then(res => {
+ if (!res.ok) {
+ throw "Received non-200 response from server: " + res.status;
+ }
+ let ct = res.headers.get('content-type');
+ if (ct.indexOf(';') !== -1) {
+ ct = ct.slice(0, ct.indexOf(';'));
+ }
+ if (ct !== "text/html") {
+ throw "Received non-HTML response " + ct;
+ }
+ return res.text();
+ })
.then(body => {
const doc = new JSDOM(body, {
url: args[0],
@@ 130,4 142,4 @@ fetch(args[0])
const readable = new JSDOM(article.content, {url: args[0]});
console.log(convert(readable.window.document, article.title));
})
- .catch(() => console.log("An error occured while fetching this page."));
+ .catch(err => console.log("An error occured while fetching this page: " + err));