From fbfc5c8a2d5b350a02fed09341761defd0078e7e Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 31 Mar 2015 22:29:15 +0100 Subject: [PATCH] Add error codes to api responses --- red/api/flows.js | 2 +- red/api/index.js | 2 +- red/api/library.js | 2 +- red/api/nodes.js | 28 +++++++++++++--------------- red/nodes/index.js | 4 +++- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/red/api/flows.js b/red/api/flows.js index 7fbcf9360..0dda455a4 100644 --- a/red/api/flows.js +++ b/red/api/flows.js @@ -34,7 +34,7 @@ module.exports = { }).otherwise(function(err) { log.warn("Error saving flows : "+err.message); log.warn(err.stack); - res.json(500,{message:err.message}); + res.json(500,{error:"unexpected_error", message:err.message}); }); } } diff --git a/red/api/index.js b/red/api/index.js index 909580f58..05a13fc55 100644 --- a/red/api/index.js +++ b/red/api/index.js @@ -32,7 +32,7 @@ var settings = require("../settings"); var errorHandler = function(err,req,res,next) { console.log(err.stack); - res.json(400,{message:err.toString()}); + res.json(400,{error:"unexpected_error", message:err.toString()}); }; function init(adminApp,storage) { diff --git a/red/api/library.js b/red/api/library.js index b89f12501..9a3964b19 100644 --- a/red/api/library.js +++ b/red/api/library.js @@ -57,7 +57,7 @@ function createLibrary(type) { res.send(403); return; } - res.json(500,{message:err.toString()}); + res.json(500,{error:"unexpected_error", message:err.toString()}); }); }); } diff --git a/red/api/nodes.js b/red/api/nodes.js index 619d6cb25..c8226731a 100644 --- a/red/api/nodes.js +++ b/red/api/nodes.js @@ -37,7 +37,7 @@ module.exports = { post: function(req,res) { if (!settings.available()) { - res.json(400,{message:"Settings unavailable"}); + res.json(400,{error:"settings_unavailable", message:"Settings unavailable"}); return; } var node = req.body; @@ -45,12 +45,12 @@ module.exports = { if (node.module) { var module = redNodes.getModuleInfo(node.module); if (module) { - res.json(400,{message:"Module already loaded"}); + res.json(400,{error:"module_already_loaded", message:"Module already loaded"}); return; } promise = server.installModule(node.module); } else { - res.json(400,{message:"Invalid request"}); + res.json(400,{error:"invalid_request", message:"Invalid request"}); return; } promise.then(function(info) { @@ -59,14 +59,14 @@ module.exports = { if (err.code === 404) { res.send(404); } else { - res.json(400,{message:err.toString()}); + res.json(400,{error:err.code||"unexpected_error", message:err.toString()}); } }); }, delete: function(req,res) { if (!settings.available()) { - res.json(400,{message:"Settings unavailable"}); + res.json(400,{error:"settings_unavailable", message:"Settings unavailable"}); return; } var mod = req.params.mod; @@ -83,12 +83,10 @@ module.exports = { promise.then(function() { res.send(204); }).otherwise(function(err) { - console.log(err.stack); - res.json(400,{message:err.toString()}); + res.json(400,{error:err.code||"unexpected_error", message:err.toString()}); }); } catch(err) { - console.log(err.stack); - res.json(400,{message:err.toString()}); + res.json(400,{error:err.code||"unexpected_error", message:err.toString()}); } }, @@ -122,12 +120,12 @@ module.exports = { putSet: function(req,res) { if (!settings.available()) { - res.json(400,{message:"Settings unavailable"}); + res.json(400,{error:"settings_unavailable", message:"Settings unavailable"}); return; } var body = req.body; if (!body.hasOwnProperty("enabled")) { - res.json(400,{message:"Invalid request"}); + res.json(400,{error:"invalid_request", message:"Invalid request"}); return; } try { @@ -141,18 +139,18 @@ module.exports = { res.json(putNode(node, body.enabled)); } } catch(err) { - res.json(400,{message:err.toString()}); + res.json(400,{error:err.code||"unexpected_error", message:err.toString()}); } }, putModule: function(req,res) { if (!settings.available()) { - res.json(400,{message:"Settings unavailable"}); + res.json(400,{error:"settings_unavailable", message:"Settings unavailable"}); return; } var body = req.body; if (!body.hasOwnProperty("enabled")) { - res.json(400,{message:"Invalid request"}); + res.json(400,{error:"invalid_request", message:"Invalid request"}); return; } try { @@ -186,7 +184,7 @@ module.exports = { } res.json(redNodes.getModuleInfo(mod)); } catch(err) { - res.json(400,{message:err.toString()}); + res.json(400,{error:err.code||"unexpected_error", message:err.toString()}); } } }; diff --git a/red/nodes/index.js b/red/nodes/index.js index 2654a9d7b..c63572aa0 100644 --- a/red/nodes/index.js +++ b/red/nodes/index.js @@ -74,7 +74,9 @@ function checkTypeInUse(id) { }); if (nodesInUse.length > 0) { var msg = nodesInUse.join(", "); - throw new Error("Type in use: "+msg); + var err = new Error("Type in use: "+msg); + err.code = "type_in_use"; + throw err; } } }