1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add error codes to api responses

This commit is contained in:
Nick O'Leary 2015-03-31 22:29:15 +01:00
parent 31b018c80e
commit fbfc5c8a2d
5 changed files with 19 additions and 19 deletions

View File

@ -34,7 +34,7 @@ module.exports = {
}).otherwise(function(err) { }).otherwise(function(err) {
log.warn("Error saving flows : "+err.message); log.warn("Error saving flows : "+err.message);
log.warn(err.stack); log.warn(err.stack);
res.json(500,{message:err.message}); res.json(500,{error:"unexpected_error", message:err.message});
}); });
} }
} }

View File

@ -32,7 +32,7 @@ var settings = require("../settings");
var errorHandler = function(err,req,res,next) { var errorHandler = function(err,req,res,next) {
console.log(err.stack); console.log(err.stack);
res.json(400,{message:err.toString()}); res.json(400,{error:"unexpected_error", message:err.toString()});
}; };
function init(adminApp,storage) { function init(adminApp,storage) {

View File

@ -57,7 +57,7 @@ function createLibrary(type) {
res.send(403); res.send(403);
return; return;
} }
res.json(500,{message:err.toString()}); res.json(500,{error:"unexpected_error", message:err.toString()});
}); });
}); });
} }

View File

@ -37,7 +37,7 @@ module.exports = {
post: function(req,res) { post: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.json(400,{message:"Settings unavailable"}); res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
return; return;
} }
var node = req.body; var node = req.body;
@ -45,12 +45,12 @@ module.exports = {
if (node.module) { if (node.module) {
var module = redNodes.getModuleInfo(node.module); var module = redNodes.getModuleInfo(node.module);
if (module) { if (module) {
res.json(400,{message:"Module already loaded"}); res.json(400,{error:"module_already_loaded", message:"Module already loaded"});
return; return;
} }
promise = server.installModule(node.module); promise = server.installModule(node.module);
} else { } else {
res.json(400,{message:"Invalid request"}); res.json(400,{error:"invalid_request", message:"Invalid request"});
return; return;
} }
promise.then(function(info) { promise.then(function(info) {
@ -59,14 +59,14 @@ module.exports = {
if (err.code === 404) { if (err.code === 404) {
res.send(404); res.send(404);
} else { } else {
res.json(400,{message:err.toString()}); res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
} }
}); });
}, },
delete: function(req,res) { delete: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.json(400,{message:"Settings unavailable"}); res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
return; return;
} }
var mod = req.params.mod; var mod = req.params.mod;
@ -83,12 +83,10 @@ module.exports = {
promise.then(function() { promise.then(function() {
res.send(204); res.send(204);
}).otherwise(function(err) { }).otherwise(function(err) {
console.log(err.stack); res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.json(400,{message:err.toString()});
}); });
} catch(err) { } catch(err) {
console.log(err.stack); res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
res.json(400,{message:err.toString()});
} }
}, },
@ -122,12 +120,12 @@ module.exports = {
putSet: function(req,res) { putSet: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.json(400,{message:"Settings unavailable"}); res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
return; return;
} }
var body = req.body; var body = req.body;
if (!body.hasOwnProperty("enabled")) { if (!body.hasOwnProperty("enabled")) {
res.json(400,{message:"Invalid request"}); res.json(400,{error:"invalid_request", message:"Invalid request"});
return; return;
} }
try { try {
@ -141,18 +139,18 @@ module.exports = {
res.json(putNode(node, body.enabled)); res.json(putNode(node, body.enabled));
} }
} catch(err) { } catch(err) {
res.json(400,{message:err.toString()}); res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
} }
}, },
putModule: function(req,res) { putModule: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.json(400,{message:"Settings unavailable"}); res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
return; return;
} }
var body = req.body; var body = req.body;
if (!body.hasOwnProperty("enabled")) { if (!body.hasOwnProperty("enabled")) {
res.json(400,{message:"Invalid request"}); res.json(400,{error:"invalid_request", message:"Invalid request"});
return; return;
} }
try { try {
@ -186,7 +184,7 @@ module.exports = {
} }
res.json(redNodes.getModuleInfo(mod)); res.json(redNodes.getModuleInfo(mod));
} catch(err) { } catch(err) {
res.json(400,{message:err.toString()}); res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
} }
} }
}; };

View File

@ -74,7 +74,9 @@ function checkTypeInUse(id) {
}); });
if (nodesInUse.length > 0) { if (nodesInUse.length > 0) {
var msg = nodesInUse.join(", "); 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;
} }
} }
} }