2017-01-23 7 views
1

ich zur Zeit auf api mit Knex arbeite. Ich habe einige Operationen in einer großen Transaktion durchzuführen und haben dort valdiate auch - wenn überhaupt Validierung gibt „false“ - Transaktion gestoppt werden muss. Das Problem ist, wenn ich es „mein“ Fehler werfen, obwohl alle „Catch“ es bekommt es und res sended corrently- gleich nach, dass meine ganze api Absturz mit Fehler:Werfen individuelle Fehler in Ursache api Knex Transaktion zum Absturz

Cannot read property "removeListener" of null

die seltsam ist, becouse es keine ähnliche Probleme mit sich Fehler durch Knex zu kontrollieren.

Strangley, wenn ich Werfen meiner Fehler entfernen würde - ich werde immer noch Ausnahme nicht behandelte

Cannot read property "rollback" of null

in Code sieht es wie folgt aus:

f1(){ 
// (...) 
    let noErrors = true; 
    return global.knex 
     .transaction((trx) => { 
      return operation1(trx) //Knex operation returning object with parameter "correct" 
      .then((output)=>{ 
       if(output.correct === false) 
         throw new Error('CustomError'); 
      }) 
      .then(()=>{ return operation2(trx); }) 
      .then(()=>{ return operation3(trx); }) 
      // (...) 
      .then(trx.commit) 
      .catch((error) => { 
        console.error('TRANS. FAILED'); 
        noErrors = false; 
        trx.rollback(); 
        throw error; // Both with and without it - failed 
       }); 
     }) 
     .then(() => { 
       console.log('TRANS. OK'); 
     }) 
     .then(() => { 
      if(noErrors) 
       return {result:'MyResultsHere'}; 
      else 
       return {result:'ErrorOccured'}; 
     }) 
     .catch((error) => { 
      return {result:'ErrorOccuredAgain'}; 
     }); 

}

dieses Ergebnis der Funktion (Versprechen) wird dann zurückgegeben:

 f1().then((output)=>{ 
        console.log(output.result); 
        // (...) sending response for request here 
      } 
      .catch((err) => { 
       console.error(err); 
       res.status(500).send(); 
      }); 

nach einiger additiona ltesting - es erscheint liek ich meine benutzerdefinierte Fehler werfen, aber hier geht es mit Rollback - und soemtiems bekomme ich einen weiteren Fehler:

TransactionError: Requests can only be made in the LoggedIn state, not the SentClientRequest state

Antwort

1

Sieht aus wie Sie sind 2 verschiedene Transaktionsabwicklung Syntaxen Misch (einfache Beispiele unten):

knex.transaction(trx => { 
    // returning promise automatically calls commit/rollback 
    return operation(1); 
}) 
.then(results => console.log("commit was called automatically", results)) 
.catch(err => console.log("rollback was called automatically", err)) 

und

knex.transaction(trx => { 
    // NOT returning promise so you need to call commit/rollback explicitly 
    operation(1).then(results => trx.commit(results)) 
    .catch(err => trx.rollback(err)); 
}) 
.then(results => console.log("stuff from transaction commit", results)) 
.catch(err => console.log("error passed to rollback", err)) 

wahrscheinlich versuchen Sie, dies zu tun:

f1(){ 
    // (...) 
    return global.knex 
    .transaction(trx => { 
     return operation1(trx) 
     .then(output => { 
      if(output.correct === false) { 
      // if wrong results promise will reject with "CustomError" 
      throw new Error('CustomError'); 
      } 
     }) 
     .then(() => operation2(trx)) 
     .then(() => operation3(trx)) 
     // (...) 
     ; 
    }) 
    .then(resultsOfLastThen => { 
     console.log('TRANS. OK', resultsOfLastOperation); 
     return { result: 'MyResultsHere' }; 
    }) 
    .catch(error => { 
     return { result: 'ErrorOccured' }; 
    }); 
} 
Verwandte Themen