mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Standardise API error response format
This commit is contained in:
		| @@ -148,7 +148,7 @@ RED.deploy = (function() { | ||||
|             }).fail(function(xhr,textStatus,err) { | ||||
|                 RED.nodes.dirty(true); | ||||
|                 if (xhr.responseText) { | ||||
|                     RED.notify("<strong>Error</strong>: "+xhr.responseText,"error"); | ||||
|                     RED.notify("<strong>Error</strong>: "+xhr.responseJSON.message,"error"); | ||||
|                 } else { | ||||
|                     RED.notify("<strong>Error</strong>: no response from server","error"); | ||||
|                 } | ||||
|   | ||||
| @@ -330,7 +330,7 @@ RED.library = (function() { | ||||
|             }).done(function(data,textStatus,xhr) { | ||||
|                 RED.notify("Saved "+options.type,"success"); | ||||
|             }).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({ | ||||
|   | ||||
| @@ -99,7 +99,6 @@ module.exports = { | ||||
|     authenticateClient: authenticateClient, | ||||
|     getToken: getToken, | ||||
|     errorHandler: function(err,req,res,next) { | ||||
|         //TODO: standardize json response | ||||
|         //TODO: audit log statment | ||||
|         //console.log(err.stack); | ||||
|         //log.log({level:"audit",type:"auth",msg:err.toString()}); | ||||
|   | ||||
| @@ -34,7 +34,7 @@ module.exports = { | ||||
|         }).otherwise(function(err) { | ||||
|             log.warn("Error saving flows : "+err.message); | ||||
|             log.warn(err.stack); | ||||
|             res.send(500,err.message); | ||||
|             res.json(500,{message:err.message}); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -31,9 +31,8 @@ var needsPermission = auth.needsPermission; | ||||
| var settings = require("../settings"); | ||||
|  | ||||
| var errorHandler = function(err,req,res,next) { | ||||
|     //TODO: standardize json response | ||||
|     console.log(err.stack); | ||||
|     res.send(400,err.toString()); | ||||
|     res.json(400,{message:err.toString()}); | ||||
| }; | ||||
|  | ||||
| function init(adminApp,storage) { | ||||
|   | ||||
| @@ -57,7 +57,7 @@ function createLibrary(type) { | ||||
|                     res.send(403); | ||||
|                     return; | ||||
|                 } | ||||
|                 res.send(500); | ||||
|                 res.json(500,{message:err.toString()}); | ||||
|             }); | ||||
|         }); | ||||
|     } | ||||
| @@ -75,6 +75,7 @@ module.exports = { | ||||
|     }, | ||||
|     get: function(req,res) { | ||||
|         storage.getFlow(req.params[0]).then(function(data) { | ||||
|             // data is already a JSON string | ||||
|             res.set('Content-Type', 'application/json'); | ||||
|             res.send(data); | ||||
|         }).otherwise(function(err) { | ||||
|   | ||||
| @@ -37,7 +37,7 @@ module.exports = { | ||||
|  | ||||
|     post: function(req,res) { | ||||
|         if (!settings.available()) { | ||||
|             res.send(400,new Error("Settings unavailable").toString()); | ||||
|             res.json(400,{message:"Settings unavailable"}); | ||||
|             return; | ||||
|         } | ||||
|         var node = req.body; | ||||
| @@ -45,12 +45,12 @@ module.exports = { | ||||
|         if (node.module) { | ||||
|             var module = redNodes.getNodeModuleInfo(node.module); | ||||
|             if (module) { | ||||
|                 res.send(400,"Module already loaded"); | ||||
|                 res.json(400,{message:"Module already loaded"}); | ||||
|                 return; | ||||
|             } | ||||
|             promise = server.installModule(node.module); | ||||
|         } else { | ||||
|             res.send(400,"Invalid request"); | ||||
|             res.json(400,{message:"Invalid request"}); | ||||
|             return; | ||||
|         } | ||||
|         promise.then(function(info) { | ||||
| @@ -59,14 +59,14 @@ module.exports = { | ||||
|             if (err.code === 404) { | ||||
|                 res.send(404); | ||||
|             } else { | ||||
|                 res.send(400,err.toString()); | ||||
|                 res.json(400,{message:err.toString()}); | ||||
|             } | ||||
|         }); | ||||
|     }, | ||||
|  | ||||
|     delete: function(req,res) { | ||||
|         if (!settings.available()) { | ||||
|             res.send(400,new Error("Settings unavailable").toString()); | ||||
|             res.json(400,{message:"Settings unavailable"}); | ||||
|             return; | ||||
|         } | ||||
|         var mod = req.params.mod; | ||||
| @@ -83,10 +83,10 @@ module.exports = { | ||||
|             promise.then(function() { | ||||
|                 res.send(204); | ||||
|             }).otherwise(function(err) { | ||||
|                 res.send(400,err.toString()); | ||||
|                 res.json(400,{message:err.toString()}); | ||||
|             }); | ||||
|         } catch(err) { | ||||
|             res.send(400,err.toString()); | ||||
|             res.json(400,{message:err.toString()}); | ||||
|         } | ||||
|     }, | ||||
|  | ||||
| @@ -120,12 +120,12 @@ module.exports = { | ||||
|  | ||||
|     putSet: function(req,res) { | ||||
|         if (!settings.available()) { | ||||
|             res.send(400,new Error("Settings unavailable").toString()); | ||||
|             res.json(400,{message:"Settings unavailable"}); | ||||
|             return; | ||||
|         } | ||||
|         var body = req.body; | ||||
|         if (!body.hasOwnProperty("enabled")) { | ||||
|             res.send(400,"Invalid request"); | ||||
|             res.json(400,{message:"Invalid request"}); | ||||
|             return; | ||||
|         } | ||||
|         try { | ||||
| @@ -138,18 +138,18 @@ module.exports = { | ||||
|                 res.json(putNode(node, body.enabled)); | ||||
|             } | ||||
|         } catch(err) { | ||||
|             res.send(400,err.toString()); | ||||
|             res.json(400,{message:err.toString()}); | ||||
|         } | ||||
|     }, | ||||
|  | ||||
|     putModule: function(req,res) { | ||||
|         if (!settings.available()) { | ||||
|             res.send(400,new Error("Settings unavailable").toString()); | ||||
|             res.json(400,{message:"Settings unavailable"}); | ||||
|             return; | ||||
|         } | ||||
|         var body = req.body; | ||||
|         if (!body.hasOwnProperty("enabled")) { | ||||
|             res.send(400,"Invalid request"); | ||||
|             res.json(400,{message:"Invalid request"}); | ||||
|             return; | ||||
|         } | ||||
|         try { | ||||
| @@ -183,7 +183,7 @@ module.exports = { | ||||
|             } | ||||
|             res.json(redNodes.getModuleInfo(mod)); | ||||
|         } catch(err) { | ||||
|             res.send(400,err.toString()); | ||||
|             res.json(400,{message:err.toString()}); | ||||
|         } | ||||
|     } | ||||
| }; | ||||
|   | ||||
| @@ -82,7 +82,7 @@ describe("flows api", function() { | ||||
|                 if (err) { | ||||
|                     throw err; | ||||
|                 } | ||||
|                 res.text.should.eql("expected error"); | ||||
|                 res.body.should.have.property("message","expected error"); | ||||
|                 done(); | ||||
|             }); | ||||
|     }); | ||||
|   | ||||
| @@ -292,7 +292,7 @@ describe("nodes api", function() { | ||||
|                         if (err) { | ||||
|                             throw err; | ||||
|                         } | ||||
|                         res.text.should.equal("Error: test error"); | ||||
|                         res.body.should.have.property("message","Error: test error"); | ||||
|                         done(); | ||||
|                     }); | ||||
|             }); | ||||
| @@ -422,7 +422,7 @@ describe("nodes api", function() { | ||||
|                         if (err) { | ||||
|                             throw err; | ||||
|                         } | ||||
|                         res.text.should.equal("Error: test error"); | ||||
|                         res.body.should.have.property("message","Error: test error"); | ||||
|                         done(); | ||||
|                     }); | ||||
|             }); | ||||
| @@ -461,8 +461,7 @@ describe("nodes api", function() { | ||||
|                     if (err) { | ||||
|                         throw err; | ||||
|                     } | ||||
|                     res.text.should.equal("Invalid request"); | ||||
|  | ||||
|                     res.body.should.have.property("message","Invalid request"); | ||||
|                     done(); | ||||
|                 }); | ||||
|         }); | ||||
| @@ -481,7 +480,7 @@ describe("nodes api", function() { | ||||
|                     if (err) { | ||||
|                         throw err; | ||||
|                     } | ||||
|                     res.text.should.equal("Invalid request"); | ||||
|                     res.body.should.have.property("message","Invalid request"); | ||||
|  | ||||
|                     done(); | ||||
|                 }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user