2017-09-12 4 views
0

Mein Frontend ist iOS Mobile App und als Backend-Datenquelle verwende ich die easy-Tabelle. Skriptcode einfügen/update wird in js-Dateien geschrieben.Ausnahmebehandlung in Azure Easy Table-Back-End

Aber wenn irgendwas in meinem Backend-Code bricht, gibt es jetzt, wie die mobile App es weiß. kann mir jemand helfen, die Ausnahmebehandlung zu implementieren, damit ich die Ausnahme an die mobile App senden kann?

 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 from feature dict 
delete requestItem["ItemsToInsert"]; 
delete requestItem["itemsToUpdate"]; 
//context.item = requestItem; 

if (itemsToInsert) { 
    promisesInsert = itemsToInsert.map(function(item) { 

     return tableRecord.insert(item).then (
       // Log the fulfillment value 
       function(val) { 
        console.log('Step 5: val: ' + val); 
        return(val); 
       } 
      ) 
      .catch (
       // Log the 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 the fulfillment value 
       function(val) { 
        console.log('Step 5: val: ' + val); 
        return(val); 
       } 
      ) 
      .catch (
       // Log the 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 an error in a promise 
     console.log('Promise completed. Vals: ' + JSON.stringify(arrVals)); 
     var arrRet = []; 
     for(var ind in arrVals) { 
      var val = arrVals[ind]; 
      //if val is a string error, don't do anything 
      console.log('Typeof Val: ' + typeof(val)) 
      if (typeof(val) === 'string') { 
       console.log("Already Inserted. Skipping.Val: " + val); 
       continue; 
      } else { 
       console.log("Item Inserted. Adding to 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(); 
}); 
+0

In obigem Code, ich an diesem Tisch Ebene Löschfunktion fehlt, und es wurde die Ausführung der Projektebene Funktion löschen. nach dem Hinzufügen table.delete (function (context) { zurückgeben context.execute(); }); und res.status (500) .send ({Fehler: 'etwas explodierte'}); im catch block von if (itemsToDelete) {funktioniert einwandfrei. – Girish

Antwort

1

Wenn Sie eine Ausnahme in der Middleware zu fangen, können Sie versuchen, diese Codezeile verwenden

res.status(500).send({ error: 'something blew up' }); 

statt return("Error: " + reason); die Fehler an den Client zu senden.

enter image description here

+0

Danke für Ihre Antwort. aber es funktioniert nicht, auch wenn ich jede catch-Anweisung durch res.status (500) .send ({error: 'etwas blies'}) ersetzt habe; immer noch sendet es mir Standard Fehlermeldung "Fehler": "Das Element existiert nicht" , wenn ich versuche, einen Datensatz zu löschen, der nicht existiert. – Girish

+0

Bitte beachten Sie, dass ich selbst grundlegende Nomenklatur von easy Tabelle und verwandte sehr neu bin. – Girish

+0

Ich versuche auch zu debuggen die Node.js Backend easy-Tabelle App mit http://mysite.azurewebsites.net/app.js/debug/ aber manchmal schlug es manchmal nicht den Debugger trifft. Ich bin mir nicht sicher, ob mein Skriptcode überhaupt läuft oder nicht – Girish

Verwandte Themen