Exception handling in Azure Easy Table back end -
my front end ios mobile app , backend data source, using easy table. insert/update script code written in js files.
but if break in backend code there way mobile app knows it. can guide me how implement exception handling can send exception mobile app?
var table = module.exports = require('azure-mobile-apps').table(); var insertmiddleware = function(req,res,next) { const util = require('util'); var bpromiserejected = false; var promises = [], promisesinsert = [], promisesupdate = []; var requestitem = req.body; var tablerecord = req.azuremobile.tables('table1'); var itemstoinsert = requestitem.itemstoinsert; var itemstoupdate = requestitem.itemstoupdate; console.log('step 1.' ); //delete records array feature dict delete requestitem["itemstoinsert"]; delete requestitem["itemstoupdate"]; //context.item = requestitem; if (itemstoinsert) { promisesinsert = itemstoinsert.map(function(item) { return tablerecord.insert(item).then ( // log fulfillment value function(val) { console.log('step 5: val: ' + val); return(val); } ) .catch ( // log rejection reason function(reason) { console.log('step 6. handle rejected promise ('+reason+') here.'); bpromiserejected = true; return("error: " + reason); } ); }); } if (itemstoupdate) { promisesupdate = itemstoupdate.map(function(item) { return tablerecord.update(item).then ( // log fulfillment value function(val) { console.log('step 5: val: ' + val); return(val); } ) .catch ( // log rejection reason function(reason) { console.log('step 6. handle rejected promise ('+reason+') here.'); bpromiserejected = true; return("error: " + reason); } ); }); } promises = promisesinsert.concat(promisesupdate); console.log('promises: ' + util.inspect(promises, false, null)); var result = promise.all(promises) .then(function(arrvals) { //found error in promise console.log('promise completed. vals: ' + json.stringify(arrvals)); var arrret = []; for(var ind in arrvals) { var val = arrvals[ind]; //if val string error, don't console.log('typeof val: ' + typeof(val)) if (typeof(val) === 'string') { console.log("already inserted. skipping.val: " + val); continue; } else { console.log("item inserted. adding array. val: " + val); arrret.push(val) } } var result = {id:"success", items: arrret}; console.log('all records inserted: ' + json.stringify(result)); return res.end(json.stringify(result)); } ) .catch( function(reason) { var retval = { id: 'error', reason: reason }; console.error("error: promise final.in catch. reason: " + reason); res.end(json.stringify(retval)); console.log('after 400 submitted'); } ); return result; } table.insert.use(insertmiddleware, table.operation); table.insert(function (context) { return context.execute(); });
when catch exception in middleware, can try use line of code
res.status(500).send({ error: 'something blew up' }); instead of return("error: " + reason); send error client.

Comments
Post a Comment