Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 92f3f9f

Browse files
Merge pull request #39 from plotly/try-catch-json-parse
wrap JSON.parse calls in try-catch blocks
2 parents eae498a + 5834183 commit 92f3f9f

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ Plotly.prototype.plot = function(data, graphOptions, callback) {
6969

7070
var req = https.request(options, function (res) {
7171
parseRes(res, function (err, body) {
72-
body = JSON.parse(body);
72+
73+
/* Try to parse the response */
74+
try {
75+
body = JSON.parse(body);
76+
} catch (e) {
77+
callback(e);
78+
}
7379

7480
if ( body['stream-status'] != undefined) {
7581
self.streamHost = url.parse(body['stream-host']).hostname;
@@ -169,13 +175,23 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) {
169175

170176
var req = https.get(options, function (res) {
171177
parseRes(res, function (err, body) {
172-
if (JSON.parse(body).error) {
173-
err = JSON.parse(body).error;
174-
callback(err);
175-
} else {
176-
var figure = JSON.parse(body).payload.figure;
178+
179+
/* Try to parse the response */
180+
try {
181+
body = JSON.parse(body);
182+
} catch (e) {
183+
callback(e);
184+
}
185+
186+
if (body.error) {
187+
callback(body.error);
188+
}
189+
190+
else {
191+
var figure = body.payload.figure;
177192
callback(null, figure);
178193
}
194+
179195
});
180196
});
181197

0 commit comments

Comments
 (0)