Skip to content

Commit

Permalink
improved fault tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
ctjong committed Apr 27, 2018
1 parent 2f0ed42 commit bdff776
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 197 deletions.
84 changes: 41 additions & 43 deletions adapters/db/mssqldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,54 +253,51 @@ module.exports =
{
ensurePoolInitialized(ctx, function()
{
_this.exec.safeExecute(ctx, function()
{
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);

var request = new pool.sql.Request(pool);
for (var key in queryParams)
var request = new pool.sql.Request(pool);
for (var key in queryParams)
{
if (!queryParams.hasOwnProperty(key))
continue;
var paramValue = queryParams[key];
if (typeof (paramValue) === "number" && Math.abs(paramValue) > 2147483647)
{
if (!queryParams.hasOwnProperty(key))
continue;
var paramValue = queryParams[key];
if (typeof (paramValue) === "number" && Math.abs(paramValue) > 2147483647)
{
request.input(key, pool.sql.BigInt, paramValue);
}
else
{
request.input(key, paramValue);
}
request.input(key, pool.sql.BigInt, paramValue);
}
else
{
request.input(key, paramValue);
}
request.query(queryString, function (err, dbResponse)
}
request.query(queryString, function (err, dbResponse)
{
if (err)
{
if (err)
if (!!completeCb)
_this.exec.safeCallback(ctx, completeCb);
console.log(err);
_this.exec.sendErrorResponse(ctx, "a07f", 500, "error while sending query to database");
}
else
{
_this.exec.safeCallback(ctx, function ()
{
if (!!completeCb)
_this.exec.safeExecute(ctx, completeCb);
console.log(err);
throw new _this.exec.Error("a07f", 500, "error while sending query to database");
}
else
successCb(dbResponse.recordset);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, function ()
{
successCb(dbResponse.recordset);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, completeCb);
}
_this.exec.safeCallback(ctx, completeCb);
}
});
console.log("-------------------------------------------------");
}
});
console.log("-------------------------------------------------");
});
}

Expand All @@ -313,7 +310,7 @@ module.exports =
{
if(!!pool)
{
callback();
_this.exec.safeCallback(ctx, callback);
return;
}
var sql = require("mssql");
Expand All @@ -322,10 +319,11 @@ module.exports =
if (err)
{
console.log(err);
throw new _this.exec.Error("f8cb", 500, "error while connecting to database");
_this.exec.sendErrorResponse(ctx, "f8cb", 500, "error while connecting to database");
return;
}
pool.sql = sql;
callback();
_this.exec.safeCallback(ctx, callback);
});
}

Expand Down
55 changes: 26 additions & 29 deletions adapters/db/mysqldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,39 +253,36 @@ module.exports =
{
ensurePoolInitialized(ctx, function()
{
_this.exec.safeExecute(ctx, function()
{
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);
var queryString = query.getQueryString();
var queryParams = query.getQueryParams();
console.log("-------------------------------------------------");
console.log("Sending query to database:");
console.log(queryString);
console.log("Query parameters:");
console.log(queryParams);

pool.query(queryString, queryParams, function (error, results, fields)
pool.query(queryString, queryParams, function (error, results, fields)
{
if (error)
{
if (error)
if (!!completeCb)
_this.exec.safeCallback(ctx, completeCb);
console.log(error);
_this.exec.sendErrorResponse(ctx, "a07f", 500, "error while sending query to database");
}
else
{
_this.exec.safeCallback(ctx, function ()
{
if (!!completeCb)
_this.exec.safeExecute(ctx, completeCb);
console.log(error);
throw new _this.exec.Error("a07f", 500, "error while sending query to database");
}
else
successCb(results);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, function ()
{
successCb(results);
});
if (!!completeCb)
{
_this.exec.safeExecute(ctx, completeCb);
}
_this.exec.safeCallback(ctx, completeCb);
}
});
console.log("-------------------------------------------------");
}
});
console.log("-------------------------------------------------");
});
}

Expand All @@ -298,7 +295,7 @@ module.exports =
{
if(!!pool)
{
callback();
_this.exec.safeCallback(ctx, callback);
return;
}
var sql = require("mysql");
Expand All @@ -322,7 +319,7 @@ module.exports =
multipleStatements: true
});
pool.sql = sql;
callback();
_this.exec.safeCallback(ctx, callback);
}

/**
Expand Down
23 changes: 10 additions & 13 deletions adapters/storage/azureStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,34 @@ module.exports =
var form = new (_this.multiparty.Form)();
form.on('part', function(stream)
{
_this.exec.safeExecute(ctx, function()
_this.exec.safeCallback(ctx, function()
{
isFirstPartReceived = true;
if (!stream.filename)
throw new _this.exec.Error("ffce", 400, "submitted file is not a valid file");
_this.exec.throwError("ffce", 400, "submitted file is not a valid file");
var size = stream.byteCount - stream.byteOffset;
var name = _this.guid() + stream.filename.substring(stream.filename.lastIndexOf("."));
provider.createBlockBlobFromStream(ctx.config.storage.azureStorageContainerName, name, stream, size,
{
contentSettings: { contentType: _this.mime.lookup(name) }
},
},
function(error)
{
callback(error, name);
_this.exec.safeCallback(ctx, function()
{
callback(error, name);
});
});
});
});
form.on('progress', function(bytesReceived, bytesExpected)
{
_this.exec.safeExecute(ctx, function()
{
if(!isFirstPartReceived && bytesReceived >= bytesExpected)
throw new _this.exec.Error("171d", 400, "error while parsing the first part");
});
if(!isFirstPartReceived && bytesReceived >= bytesExpected)
_this.exec.sendErrorResponse(ctx, "171d", 400, "error while parsing the first part");
});
form.on('error', function(err)
{
_this.exec.safeExecute(ctx, function()
{
throw new _this.exec.Error("ead9", 400, "error while parsing form data");
});
_this.exec.sendErrorResponse(ctx, "ead9", 400, "error while parsing form data");
});
form.parse(req);
}
Expand Down
7 changes: 2 additions & 5 deletions adapters/storage/localHostStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports =
});
form.on('file', function(name, file)
{
_this.exec.safeExecute(ctx, function()
_this.exec.safeCallback(ctx, function()
{
var tempPath = file.path;
var tempName = path.basename(tempPath);
Expand All @@ -59,10 +59,7 @@ module.exports =
});
form.on('error', function(err)
{
_this.exec.safeExecute(ctx, function()
{
throw new _this.exec.Error("8651", 400, "error while parsing form data");
});
_this.exec.sendErrorResponse(ctx, "8651", 400, "error while parsing form data");
});
form.parse(req);
}
Expand Down
16 changes: 5 additions & 11 deletions adapters/storage/s3Storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ module.exports =
});
form.on('part', function(stream)
{
_this.exec.safeExecute(ctx, function()
_this.exec.safeCallback(ctx, function()
{
isFirstPartReceived = true;
if (!stream.filename)
throw new _this.exec.Error("8dad", 400, "submitted file is not a valid file");
_this.exec.throwError("8dad", 400, "submitted file is not a valid file");
var name = _this.guid() + stream.filename.substring(stream.filename.lastIndexOf("."));
provider.upload(
{
Expand All @@ -72,18 +72,12 @@ module.exports =
});
form.on('progress', function(bytesReceived, bytesExpected)
{
_this.exec.safeExecute(ctx, function()
{
if(!isFirstPartReceived && bytesReceived >= bytesExpected)
throw new _this.exec.Error("49ef", 400, "error while parsing the first part");
});
if(!isFirstPartReceived && bytesReceived >= bytesExpected)
_this.exec.sendErrorResponse(ctx, "49ef", 400, "error while parsing the first part");
});
form.on('error', function(err)
{
_this.exec.safeExecute(ctx, function()
{
throw new _this.exec.Error("a95a", 400, "error while parsing form data");
});
_this.exec.sendErrorResponse(ctx, "a95a", 400, "error while parsing form data");
});
form.parse(req);
}
Expand Down
16 changes: 8 additions & 8 deletions handlers/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ module.exports =
var requiredField = requiredFields[i];
if(!requestBody.hasOwnProperty(requiredField) || !requestBody[requiredField])
{
throw new _this.exec.Error("86c5", 400, "missing required field " + requiredFields[i]);
_this.exec.throwError("86c5", 400, "missing required field " + requiredFields[i]);
}
}
if(ctx.entity === "user")
{
_this.auth.verifyAuthSupported(ctx);
if(requestBody.password !== requestBody.confirmpassword)
{
throw new _this.exec.Error("1b9e", 400, "password doesn't match the confirmation");
_this.exec.throwError("1b9e", 400, "password doesn't match the confirmation");
}
validateEmail(requestBody.email);
verifyPwdRequirements(requestBody.password, ctx.config.auth.passwordReqs);
Expand Down Expand Up @@ -173,7 +173,7 @@ module.exports =
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!re.test(email))
{
throw new _this.exec.Error("dbaa", 400, "email is not valid: " + email);
_this.exec.throwError("dbaa", 400, "email is not valid: " + email);
}
}

Expand All @@ -186,23 +186,23 @@ module.exports =
{
if(newPassword.length < passwordReqs.minLength)
{
throw new _this.exec.Error("ef60", 400, "password must be at least " + passwordReqs.minLength + " characters long");
_this.exec.throwError("ef60", 400, "password must be at least " + passwordReqs.minLength + " characters long");
}
if (passwordReqs.lowercaseChar && !newPassword.match(/[a-z]/))
{
throw new _this.exec.Error("8c5e", 400, "password must contain at least one lowercase character.");
_this.exec.throwError("8c5e", 400, "password must contain at least one lowercase character.");
}
if(passwordReqs.uppercaseChar && !newPassword.match(/[A-Z]/))
{
throw new _this.exec.Error("d625", 400, "password must contain at least one uppercase character.");
_this.exec.throwError("d625", 400, "password must contain at least one uppercase character.");
}
if (passwordReqs.digitChar && !newPassword.match(/[0-9]/))
{
throw new _this.exec.Error("6db6", 400, "password must contain at least one numeric character.");
_this.exec.throwError("6db6", 400, "password must contain at least one numeric character.");
}
if (passwordReqs.specialChar && !newPassword.match(/[!#$%&()*+,-./:;<=>?@[\]^_`{|}~]/))
{
throw new _this.exec.Error("8b26", 400, "password must contain at least one special character (!#$%&()*+,-./:;<=>?@[\]^_`{|}~).");
_this.exec.throwError("8b26", 400, "password must contain at least one special character (!#$%&()*+,-./:;<=>?@[\]^_`{|}~).");
}
}

Expand Down
25 changes: 11 additions & 14 deletions handlers/createAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,24 @@ module.exports =
function execute(ctx, req)
{
if (!ctx.config.storage)
throw new _this.exec.Error("e668", 500, "file upload is not supported for this site");
_this.exec.throwError("e668", 500, "file upload is not supported for this site");
if(!ctx.userId)
throw new _this.exec.Error("4f53", 401, "anonymous upload is not supported");
_this.exec.throwError("4f53", 401, "anonymous upload is not supported");
_this.helper.onBeginWriteRequest(ctx, "create", _this.db, null, { "ownerid": ctx.userId }, function(record, requestBody)
{
_this.storage.uploadFile(ctx, req, function (error, name)
{
_this.exec.safeExecute(ctx, function()
if (error)
{
if (error)
{
throw new _this.exec.Error("d2d0", 500, "error while uploading file to storage system");
}
else
_this.exec.sendErrorResponse(ctx, "d2d0", 500, "error while uploading file to storage system");
}
else
{
_this.db.insert(ctx, "asset", ["ownerid", "filename"], [ctx.userId, name], function (insertedId)
{
_this.db.insert(ctx, "asset", ["ownerid", "filename"], [ctx.userId, name], function (insertedId)
{
ctx.res.send(insertedId.toString());
});
}
});
ctx.res.send(insertedId.toString());
});
}
});
});
}
Expand Down
Loading

0 comments on commit bdff776

Please sign in to comment.