Standardise API error response format

This commit is contained in:
Nick O'Leary 2015-03-30 14:16:04 +01:00
parent 7adefd6ee0
commit 2a8a885271
9 changed files with 24 additions and 26 deletions

View File

@ -148,7 +148,7 @@ RED.deploy = (function() {
}).fail(function(xhr,textStatus,err) { }).fail(function(xhr,textStatus,err) {
RED.nodes.dirty(true); RED.nodes.dirty(true);
if (xhr.responseText) { if (xhr.responseText) {
RED.notify("<strong>Error</strong>: "+xhr.responseText,"error"); RED.notify("<strong>Error</strong>: "+xhr.responseJSON.message,"error");
} else { } else {
RED.notify("<strong>Error</strong>: no response from server","error"); RED.notify("<strong>Error</strong>: no response from server","error");
} }

View File

@ -330,7 +330,7 @@ RED.library = (function() {
}).done(function(data,textStatus,xhr) { }).done(function(data,textStatus,xhr) {
RED.notify("Saved "+options.type,"success"); RED.notify("Saved "+options.type,"success");
}).fail(function(xhr,textStatus,err) { }).fail(function(xhr,textStatus,err) {
RED.notify("Saved failed: "+xhr.responseText,"error"); RED.notify("Saved failed: "+xhr.responseJSON.message,"error");
}); });
} }
$( "#node-dialog-library-save-confirm" ).dialog({ $( "#node-dialog-library-save-confirm" ).dialog({

View File

@ -99,7 +99,6 @@ module.exports = {
authenticateClient: authenticateClient, authenticateClient: authenticateClient,
getToken: getToken, getToken: getToken,
errorHandler: function(err,req,res,next) { errorHandler: function(err,req,res,next) {
//TODO: standardize json response
//TODO: audit log statment //TODO: audit log statment
//console.log(err.stack); //console.log(err.stack);
//log.log({level:"audit",type:"auth",msg:err.toString()}); //log.log({level:"audit",type:"auth",msg:err.toString()});

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.send(500,err.message); res.json(500,{message:err.message});
}); });
} }
} }

View File

@ -31,9 +31,8 @@ var needsPermission = auth.needsPermission;
var settings = require("../settings"); var settings = require("../settings");
var errorHandler = function(err,req,res,next) { var errorHandler = function(err,req,res,next) {
//TODO: standardize json response
console.log(err.stack); console.log(err.stack);
res.send(400,err.toString()); res.json(400,{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.send(500); res.json(500,{message:err.toString()});
}); });
}); });
} }
@ -75,6 +75,7 @@ module.exports = {
}, },
get: function(req,res) { get: function(req,res) {
storage.getFlow(req.params[0]).then(function(data) { storage.getFlow(req.params[0]).then(function(data) {
// data is already a JSON string
res.set('Content-Type', 'application/json'); res.set('Content-Type', 'application/json');
res.send(data); res.send(data);
}).otherwise(function(err) { }).otherwise(function(err) {

View File

@ -37,7 +37,7 @@ module.exports = {
post: function(req,res) { post: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.send(400,new Error("Settings unavailable").toString()); res.json(400,{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.getNodeModuleInfo(node.module); var module = redNodes.getNodeModuleInfo(node.module);
if (module) { if (module) {
res.send(400,"Module already loaded"); res.json(400,{message:"Module already loaded"});
return; return;
} }
promise = server.installModule(node.module); promise = server.installModule(node.module);
} else { } else {
res.send(400,"Invalid request"); res.json(400,{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.send(400,err.toString()); res.json(400,{message:err.toString()});
} }
}); });
}, },
delete: function(req,res) { delete: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.send(400,new Error("Settings unavailable").toString()); res.json(400,{message:"Settings unavailable"});
return; return;
} }
var mod = req.params.mod; var mod = req.params.mod;
@ -83,10 +83,10 @@ module.exports = {
promise.then(function() { promise.then(function() {
res.send(204); res.send(204);
}).otherwise(function(err) { }).otherwise(function(err) {
res.send(400,err.toString()); res.json(400,{message:err.toString()});
}); });
} catch(err) { } catch(err) {
res.send(400,err.toString()); res.json(400,{message:err.toString()});
} }
}, },
@ -120,12 +120,12 @@ module.exports = {
putSet: function(req,res) { putSet: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.send(400,new Error("Settings unavailable").toString()); res.json(400,{message:"Settings unavailable"});
return; return;
} }
var body = req.body; var body = req.body;
if (!body.hasOwnProperty("enabled")) { if (!body.hasOwnProperty("enabled")) {
res.send(400,"Invalid request"); res.json(400,{message:"Invalid request"});
return; return;
} }
try { try {
@ -138,18 +138,18 @@ module.exports = {
res.json(putNode(node, body.enabled)); res.json(putNode(node, body.enabled));
} }
} catch(err) { } catch(err) {
res.send(400,err.toString()); res.json(400,{message:err.toString()});
} }
}, },
putModule: function(req,res) { putModule: function(req,res) {
if (!settings.available()) { if (!settings.available()) {
res.send(400,new Error("Settings unavailable").toString()); res.json(400,{message:"Settings unavailable"});
return; return;
} }
var body = req.body; var body = req.body;
if (!body.hasOwnProperty("enabled")) { if (!body.hasOwnProperty("enabled")) {
res.send(400,"Invalid request"); res.json(400,{message:"Invalid request"});
return; return;
} }
try { try {
@ -183,7 +183,7 @@ module.exports = {
} }
res.json(redNodes.getModuleInfo(mod)); res.json(redNodes.getModuleInfo(mod));
} catch(err) { } catch(err) {
res.send(400,err.toString()); res.json(400,{message:err.toString()});
} }
} }
}; };

View File

@ -82,7 +82,7 @@ describe("flows api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.eql("expected error"); res.body.should.have.property("message","expected error");
done(); done();
}); });
}); });

View File

@ -292,7 +292,7 @@ describe("nodes api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.equal("Error: test error"); res.body.should.have.property("message","Error: test error");
done(); done();
}); });
}); });
@ -422,7 +422,7 @@ describe("nodes api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.equal("Error: test error"); res.body.should.have.property("message","Error: test error");
done(); done();
}); });
}); });
@ -461,8 +461,7 @@ describe("nodes api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.equal("Invalid request"); res.body.should.have.property("message","Invalid request");
done(); done();
}); });
}); });
@ -481,7 +480,7 @@ describe("nodes api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.equal("Invalid request"); res.body.should.have.property("message","Invalid request");
done(); done();
}); });