From f73e21b7a480506a0d088a318649a9bf679c99a6 Mon Sep 17 00:00:00 2001 From: Andrew Oppenlander Date: Mon, 17 Nov 2014 15:30:47 -0500 Subject: [PATCH 1/4] plotly.saveImage will now always call back, and will ensure a callback exists before trying to call it. --- index.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 8fa51ab..f0479fb 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,7 @@ Plotly.prototype.plot = function(data, graphOptions, callback) { error: body.error }); } - + }); }); @@ -167,7 +167,7 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { var figure = JSON.parse(body).payload.figure; callback(null, figure); } - + }) }); @@ -176,6 +176,12 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { Plotly.prototype.saveImage = function (figure, path, callback) { var self = this; + if(!callback) { + callback = function(err) { + if(err) { console.error(err); } + else { console.log('image saved!') ;} + }; + } figure = JSON.stringify(figure); var headers = { @@ -206,9 +212,9 @@ Plotly.prototype.saveImage = function (figure, path, callback) { } else { var image = JSON.parse(body).payload; writeFile(path, image, function (err) { - if (err) callback(err); - console.log('image saved!'); - }) + if (err) return callback(err); + callback(null); + }); } }); } @@ -216,7 +222,7 @@ Plotly.prototype.saveImage = function (figure, path, callback) { req.write(figure); req.end(); -} +}; // helper fn to create folders if they don't exist in the path From 3cc9bd7699023ae6927aa09d302d2995e58b79c2 Mon Sep 17 00:00:00 2001 From: Andrew Oppenlander Date: Mon, 17 Nov 2014 15:36:50 -0500 Subject: [PATCH 2/4] Spaaaaaaace --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index f0479fb..7f1d714 100644 --- a/index.js +++ b/index.js @@ -176,12 +176,12 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { Plotly.prototype.saveImage = function (figure, path, callback) { var self = this; - if(!callback) { - callback = function(err) { - if(err) { console.error(err); } - else { console.log('image saved!') ;} - }; - } + if(!callback) { + callback = function(err) { + if(err) { console.error(err); } + else { console.log('image saved!') ;} + }; + } figure = JSON.stringify(figure); var headers = { @@ -213,7 +213,7 @@ Plotly.prototype.saveImage = function (figure, path, callback) { var image = JSON.parse(body).payload; writeFile(path, image, function (err) { if (err) return callback(err); - callback(null); + callback(null); }); } }); From 0ef7b2a29ce8a4b02d50627ec187865c2ae10fa3 Mon Sep 17 00:00:00 2001 From: Andrew Oppenlander Date: Mon, 17 Nov 2014 15:41:44 -0500 Subject: [PATCH 3/4] Removed the console logs, since they might not be wanted by a user. --- index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/index.js b/index.js index 7f1d714..bfdb312 100644 --- a/index.js +++ b/index.js @@ -176,12 +176,7 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { Plotly.prototype.saveImage = function (figure, path, callback) { var self = this; - if(!callback) { - callback = function(err) { - if(err) { console.error(err); } - else { console.log('image saved!') ;} - }; - } + if(!callback) { callback = function() {}; } figure = JSON.stringify(figure); var headers = { From 9ef807bd0ce16e0dc05f4b04a7daede46cf7dac6 Mon Sep 17 00:00:00 2001 From: Andrew Oppenlander Date: Wed, 19 Nov 2014 16:20:32 -0500 Subject: [PATCH 4/4] Handling errors correctly in saveImage --- index.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index bfdb312..77025e3 100644 --- a/index.js +++ b/index.js @@ -161,18 +161,17 @@ Plotly.prototype.getFigure = function (fileOwner, fileId, callback) { var req = https.get(options, function (res) { parseRes(res, function (err, body) { if (JSON.parse(body).error) { - var err = JSON.parse(body).error; - callback(err); + callback(JSON.parse(body).error); } else { var figure = JSON.parse(body).payload.figure; callback(null, figure); } - }) + }); }); req.end(); -} +}; Plotly.prototype.saveImage = function (figure, path, callback) { var self = this; @@ -197,17 +196,23 @@ Plotly.prototype.saveImage = function (figure, path, callback) { agent: false }; + var _errored = false; var req = https.request(options, function (res) { if (res.statusCode !== 200) { callback(res.statusCode); } else { parseRes(res, function (err, body) { + if(_errored) { return; } if (err) { + _errored = true; callback(err); } else { var image = JSON.parse(body).payload; writeFile(path, image, function (err) { - if (err) return callback(err); + if (err) { + _errored = true; + return callback(err); + } callback(null); }); } @@ -215,6 +220,12 @@ Plotly.prototype.saveImage = function (figure, path, callback) { } }); + req.on('error', function(err) { + if(_errored) { return; } + _errored = true; + callback(err.message); + }); + req.write(figure); req.end(); }; @@ -223,10 +234,10 @@ Plotly.prototype.saveImage = function (figure, path, callback) { // helper fn to create folders if they don't exist in the path function writeFile (path, image, callback) { mkdirp(getDirName(path), function (err) { - if (err) return callback(err) + if (err) return callback(err); fs.writeFile(path + '.png', image, 'base64', function () { callback(null); - }) + }); }); }