replaced all stop callbacks with customError callbacks. Fixes #270

This commit is contained in:
Peter 'Pita' Martischka 2011-12-10 16:46:47 +01:00
parent 22fd5ae33d
commit 6684d6ed52
7 changed files with 59 additions and 38 deletions

View file

@ -19,6 +19,7 @@
*/
var ERR = require("async-stacktrace");
var customError = require("../utils/customError");
var padManager = require("./PadManager");
var padMessageHandler = require("../handler/PadMessageHandler");
var readOnlyManager = require("./ReadOnlyManager");
@ -88,7 +89,7 @@ exports.getText = function(padID, rev, callback)
}
else
{
callback({stop: "rev is not a number"});
callback(new customError("rev is not a number", "apierror"));
return;
}
}
@ -96,14 +97,14 @@ exports.getText = function(padID, rev, callback)
//ensure this is not a negativ number
if(rev !== undefined && rev < 0)
{
callback({stop: "rev is a negativ number"});
callback(new customError("rev is a negativ number","apierror"));
return;
}
//ensure this is not a float value
if(rev !== undefined && !is_int(rev))
{
callback({stop: "rev is a float value"});
callback(new customError("rev is a float value","apierror"));
return;
}
@ -118,7 +119,7 @@ exports.getText = function(padID, rev, callback)
//check if this is a valid revision
if(rev > pad.getHeadRevisionNumber())
{
callback({stop: "rev is higher than the head revision of the pad"});
callback(new customError("rev is higher than the head revision of the pad","apierror"));
return;
}
@ -188,20 +189,20 @@ exports.getHTML = function(padID, rev, callback)
}
else
{
callback({stop: "rev is not a number"});
callback(new customError("rev is not a number","apierror"));
return;
}
}
if(rev !== undefined && rev < 0)
{
callback({stop: "rev is a negative number"});
callback(new customError("rev is a negative number","apierror"));
return;
}
if(rev !== undefined && !is_int(rev))
{
callback({stop: "rev is a float value"});
callback(new customError("rev is a float value","apierror"));
return;
}
@ -215,7 +216,7 @@ exports.getHTML = function(padID, rev, callback)
//check if this is a valid revision
if(rev > pad.getHeadRevisionNumber())
{
callback({stop: "rev is higher than the head revision of the pad"});
callback(new customError("rev is higher than the head revision of the pad","apierror"));
return;
}
@ -294,7 +295,7 @@ exports.createPad = function(padID, text, callback)
//ensure there is no $ in the padID
if(padID.indexOf("$") != -1)
{
callback({stop: "createPad can't create group pads"});
callback(new customError("createPad can't create group pads","apierror"));
return;
}
@ -361,7 +362,7 @@ exports.setPublicStatus = function(padID, publicStatus, callback)
//ensure this is a group pad
if(padID.indexOf("$") == -1)
{
callback({stop: "You can only get/set the publicStatus of pads that belong to a group"});
callback(new customError("You can only get/set the publicStatus of pads that belong to a group","apierror"));
return;
}
@ -394,7 +395,7 @@ exports.getPublicStatus = function(padID, callback)
//ensure this is a group pad
if(padID.indexOf("$") == -1)
{
callback({stop: "You can only get/set the publicStatus of pads that belong to a group"});
callback(new customError("You can only get/set the publicStatus of pads that belong to a group","apierror"));
return;
}
@ -420,7 +421,7 @@ exports.setPassword = function(padID, password, callback)
//ensure this is a group pad
if(padID.indexOf("$") == -1)
{
callback({stop: "You can only get/set the password of pads that belong to a group"});
callback(new customError("You can only get/set the password of pads that belong to a group","apierror"));
return;
}
@ -449,7 +450,7 @@ exports.isPasswordProtected = function(padID, callback)
//ensure this is a group pad
if(padID.indexOf("$") == -1)
{
callback({stop: "You can only get/set the password of pads that belong to a group"});
callback(new customError("You can only get/set the password of pads that belong to a group","apierror"));
return;
}
@ -484,14 +485,14 @@ function getPadSafe(padID, shouldExist, text, callback)
//check if padID is a string
if(typeof padID != "string")
{
callback({stop: "padID is not a string"});
callback(new customError("padID is not a string","apierror"));
return;
}
//check if the padID maches the requirements
if(!padManager.isValidPadId(padID))
{
callback({stop: "padID did not match requirements"});
callback(new customError("padID did not match requirements","apierror"));
return;
}
@ -503,12 +504,12 @@ function getPadSafe(padID, shouldExist, text, callback)
//does not exist, but should
if(exists == false && shouldExist == true)
{
callback({stop: "padID does not exist"});
callback(new customError("padID does not exist","apierror"));
}
//does exists, but shouldn't
else if(exists == true && shouldExist == false)
{
callback({stop: "padID does already exist"});
callback(new customError("padID does already exist","apierror"));
}
//pad exists, let's get it
else

View file

@ -19,6 +19,7 @@
*/
var ERR = require("async-stacktrace");
var customError = require("../utils/customError");
var db = require("./DB").db;
var async = require("async");
var padManager = require("./PadManager");
@ -40,7 +41,7 @@ exports.deleteGroup = function(groupID, callback)
//group does not exist
if(_group == null)
{
callback({stop: "groupID does not exist"});
callback(new customError("groupID does not exist","apierror"));
}
//group exists, everything is fine
else
@ -135,7 +136,7 @@ exports.createGroupIfNotExistsFor = function(groupMapper, callback)
//ensure mapper is optional
if(typeof groupMapper != "string")
{
callback({stop: "groupMapper is no string"});
callback(new customError("groupMapper is no string","apierror"));
return;
}
@ -182,7 +183,7 @@ exports.createGroupPad = function(groupID, padName, text, callback)
//group does not exist
if(exists == false)
{
callback({stop: "groupID does not exist"});
callback(new customError("groupID does not exist","apierror"));
}
//group exists, everything is fine
else
@ -201,7 +202,7 @@ exports.createGroupPad = function(groupID, padName, text, callback)
//pad exists already
if(exists == true)
{
callback({stop: "padName does already exist"});
callback(new customError("padName does already exist","apierror"));
}
//pad does not exist, everything is fine
else
@ -241,7 +242,7 @@ exports.listPads = function(groupID, callback)
//group does not exist
if(exists == false)
{
callback({stop: "groupID does not exist"});
callback(new customError("groupID does not exist","apierror"));
}
//group exists, let's get the pads
else

View file

@ -19,6 +19,7 @@
*/
var ERR = require("async-stacktrace");
var customError = require("../utils/customError");
require("../db/Pad");
var db = require("./DB").db;
@ -47,7 +48,7 @@ exports.getPad = function(id, text, callback)
//check if this is a valid padId
if(!exports.isValidPadId(id))
{
callback({stop: id + " is not a valid padId"});
callback(new customError(id + " is not a valid padId","apierror"));
return;
}
@ -64,14 +65,14 @@ exports.getPad = function(id, text, callback)
//check if text is a string
if(typeof text != "string")
{
callback({stop: "text is not a string"});
callback(new customError("text is not a string","apierror"));
return;
}
//check if text is less than 100k chars
if(text.length > 100000)
{
callback({stop: "text must be less than 100k chars"});
callback(new customError("text must be less than 100k chars","apierror"));
return;
}
}

View file

@ -118,7 +118,7 @@ exports.checkAccess = function (padID, sessionID, token, password, callback)
sessionManager.getSessionInfo(sessionID, function(err, sessionInfo)
{
//skip session validation if the session doesn't exists
if(err && err.stop == "sessionID does not exist")
if(err && err.message == "sessionID does not exist")
{
callback();
return;

View file

@ -19,6 +19,7 @@
*/
var ERR = require("async-stacktrace");
var customError = require("../utils/customError");
var db = require("./DB").db;
var async = require("async");
var groupMangager = require("./GroupManager");
@ -52,7 +53,7 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
//group does not exist
if(exists == false)
{
callback({stop: "groupID does not exist"});
callback(new customError("groupID does not exist","apierror"));
}
//everything is fine, continue
else
@ -71,7 +72,7 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
//author does not exist
if(exists == false)
{
callback({stop: "authorID does not exist"});
callback(new customError("authorID does not exist","apierror"));
}
//everything is fine, continue
else
@ -93,7 +94,7 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
}
else
{
callback({stop: "validUntil is not a number"});
callback(new customError("validUntil is not a number","apierror"));
return;
}
}
@ -101,21 +102,21 @@ exports.createSession = function(groupID, authorID, validUntil, callback)
//ensure this is not a negativ number
if(validUntil < 0)
{
callback({stop: "validUntil is a negativ number"});
callback(new customError("validUntil is a negativ number","apierror"));
return;
}
//ensure this is not a float value
if(!is_int(validUntil))
{
callback({stop: "validUntil is a float value"});
callback(new customError("validUntil is a float value","apierror"));
return;
}
//check if validUntil is in the future
if(Math.floor(new Date().getTime()/1000) > validUntil)
{
callback({stop: "validUntil is in the past"});
callback(new customError("validUntil is in the past","apierror"));
return;
}
@ -192,7 +193,7 @@ exports.getSessionInfo = function(sessionID, callback)
//session does not exists
if(session == null)
{
callback({stop: "sessionID does not exist"})
callback(new customError("sessionID does not exist","apierror"))
}
//everything is fine, return the sessioninfos
else
@ -221,7 +222,7 @@ exports.deleteSession = function(sessionID, callback)
//session does not exists
if(session == null)
{
callback({stop: "sessionID does not exist"})
callback(new customError("sessionID does not exist","apierror"))
}
//everything is fine, return the sessioninfos
else
@ -285,7 +286,7 @@ exports.listSessionsOfGroup = function(groupID, callback)
//group does not exist
if(exists == false)
{
callback({stop: "groupID does not exist"});
callback(new customError("groupID does not exist","apierror"));
}
//everything is fine, continue
else
@ -304,7 +305,7 @@ exports.listSessionsOfAuthor = function(authorID, callback)
//group does not exist
if(exists == false)
{
callback({stop: "authorID does not exist"});
callback(new customError("authorID does not exist","apierror"));
}
//everything is fine, continue
else

View file

@ -115,9 +115,9 @@ exports.handle = function(functionName, fields, req, res)
res.send({code: 0, message: "ok", data: data});
}
// parameters were wrong and the api stopped execution, pass the error
else if(err.stop)
else if(err.name == "apierror")
{
res.send({code: 1, message: err.stop, data: null});
res.send({code: 1, message: err.message, data: null});
}
//an unkown error happend
else

17
node/utils/customError.js Normal file
View file

@ -0,0 +1,17 @@
/*
This helper modules allows us to create different type of errors we can throw
*/
function customError(message, errorName)
{
this.name = errorName || "Error";
this.message = message;
var stackParts = new Error().stack.split("\n");
stackParts.splice(0,2);
stackParts.unshift(this.name + ": " + message);
this.stack = stackParts.join("\n");
}
customError.prototype = Error.prototype;
module.exports = customError;