Skip to content

Commit 02a3d73

Browse files
committed
Better error handling.
1 parent 67db02c commit 02a3d73

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"devDependencies": {
2828
"chai": "^2.1.*",
2929
"mocha": "^2.1.*",
30+
"object-assign": "^2.0.0",
3031
"sinon": "^1.12.*",
3132
"supertest": "^0.15.*"
3233
}

scrape.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var phantomjs = require("phantomjs");
33
var binPath = phantomjs.path;
44
var path = require("path");
55
var Promise = require("bluebird");
6+
var objectAssign = require("object-assign");
67

78
var readabilityPath = process.env.READABILITY_LIB_PATH ||
89
path.normalize(path.join(__dirname, "vendor", "Readability.js"));
@@ -33,7 +34,7 @@ module.exports = function scrape(url, options) {
3334
error = response.error;
3435
}
3536
if (error) {
36-
reject({error: error});
37+
reject(objectAssign(new Error(), error));
3738
} else {
3839
fulfill(response);
3940
}

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ app.get("/api/get", function(req, res) {
4646
res.json(sanitize ? sanitizeResult(result) : result);
4747
}).catch(function(err) {
4848
console.log(err);
49-
res.status(500).json(err);
49+
res.status(500).json({error: JSON.parse(JSON.stringify(err))});
5050
});
5151
});
5252

static/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<div class="page-header">
1212
<h1>Readability.js <small>test page</small></h1>
1313
</div>
14+
<div class="alert alert-danger hide" id="error"></div>
1415
<div class="row">
1516
<div class="col-md-6">
1617
<form class="form-horizontal" id="form">

static/main.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var q = document.querySelector.bind(document);
55

66
function injectReadableContents(params, target) {
7+
q("#error").classList.add("hide");
78
var req = new XMLHttpRequest();
89
var apiUrl = [
910
"/api/get?sanitize=" + (params.sanitize ? "yes" : "no"),
@@ -13,12 +14,24 @@
1314
req.open("GET", apiUrl, false);
1415
req.send(null);
1516
var jsonResponse = JSON.parse(req.responseText);
16-
q("#title").textContent = jsonResponse.title;
17-
q("#byline").textContent = jsonResponse.byline;
18-
q("#length").textContent = jsonResponse.length;
19-
q("#dir").textContent = jsonResponse.dir;
20-
q("#excerpt").textContent = jsonResponse.excerpt;
21-
target.contentDocument.body.innerHTML = jsonResponse.content;
17+
if (jsonResponse.error) {
18+
q("#error").textContent = jsonResponse.error.message;
19+
q("#error").classList.remove("hide");
20+
q("#title").textContent = "";
21+
q("#byline").textContent = "";
22+
q("#length").textContent = "";
23+
q("#dir").textContent = "";
24+
q("#excerpt").textContent = "";
25+
target.contentDocument.body.innerHTML = "";
26+
} else {
27+
q("#error").textContent = "";
28+
q("#title").textContent = jsonResponse.title;
29+
q("#byline").textContent = jsonResponse.byline;
30+
q("#length").textContent = jsonResponse.length;
31+
q("#dir").textContent = jsonResponse.dir;
32+
q("#excerpt").textContent = jsonResponse.excerpt;
33+
target.contentDocument.body.innerHTML = jsonResponse.content;
34+
}
2235
}
2336

2437
function init() {

test/index.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,31 @@ describe("Tests", function() {
5757
});
5858

5959
scrape("http://invalid.test/").catch(function(err) {
60-
expect(err.error.message).to.match(/Unable to parse JSON proxy response/);
60+
expect(err.message).to.match(/Unable to parse JSON proxy response/);
61+
done();
62+
});
63+
});
64+
65+
it("should reject on data extraction error", function(done) {
66+
sandbox.stub(childProcess, "execFile", function(exec, args, cb) {
67+
cb(null, JSON.stringify({error: {message: "Foo"}}));
68+
});
69+
70+
scrape("http://invalid.test/").catch(function(err) {
71+
expect(err).to.be.an.instanceOf(Error);
72+
expect(err.message).eql("Foo");
6173
done();
6274
});
6375
});
6476

6577
it("should fulfill with a valid json result", function(done) {
6678
sandbox.stub(childProcess, "execFile", function(exec, args, cb) {
67-
cb(null, JSON.stringify({title: "plop"}));
79+
cb(null, JSON.stringify({title: "plop", content: "plip"}));
6880
});
6981

7082
scrape("http://invalid.test/").then(function(result) {
7183
expect(result.title).eql("plop");
84+
expect(result.content).eql("plip");
7285
done();
7386
});
7487
});
@@ -131,6 +144,20 @@ describe("Tests", function() {
131144
.end(done);
132145
});
133146

147+
it("should return a server error on call error", function(done) {
148+
sandbox.stub(childProcess, "execFile", function(exec, args, cb) {
149+
cb(null, JSON.stringify({error: {message: "fail"}}));
150+
});
151+
152+
request(app)
153+
.get("/api/get?url=http://invalid.test/")
154+
.expect(500)
155+
.expect(function(res) {
156+
expect(res.body.error.message).eql("fail");
157+
})
158+
.end(done);
159+
});
160+
134161
it("should apply custom user agent when provided", function(done) {
135162
sandbox.stub(childProcess, "execFile", function(exec, args, cb) {
136163
cb(null, "{}");

0 commit comments

Comments
 (0)