From 0aa17662f5d09d6b2c538f0b79d384524953057a Mon Sep 17 00:00:00 2001 From: Nicholas O'Leary Date: Fri, 22 Nov 2013 21:04:05 +0000 Subject: [PATCH] Posting to /flows should block until successfully saved, or fail Mentioned in #76 --- red/server.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/red/server.js b/red/server.js index bfbe17f55..e292a36ee 100644 --- a/red/server.js +++ b/red/server.js @@ -49,14 +49,20 @@ function createServer(_server,_settings) { fullBody += chunk.toString(); }); req.on('end', function() { - res.writeHead(204, {'Content-Type': 'text/plain'}); - res.end(); - var flows = JSON.parse(fullBody); - storage.saveFlows(flows).then(function() { - redNodes.setConfig(flows); - }).otherwise(function(err) { + try { + var flows = JSON.parse(fullBody); + storage.saveFlows(flows).then(function() { + res.writeHead(204, {'Content-Type': 'text/plain'}); + res.end(); + redNodes.setConfig(flows); + }).otherwise(function(err) { + util.log("[red] Error saving flows : "+err); + res.send(500, err.message); + }); + } catch(err) { util.log("[red] Error saving flows : "+err); - }); + res.send(400, "Invalid flow"); + } }); }); }