在 indexedDB 中捕获错误和回滚

Catching errors and rollbacks in indexedDB

我一定遗漏了一些非常基本的东西,如果您能提供任何指导,我将不胜感激。谢谢。

以下代码是我认为在 catch 中带有中止事务的注释掉的 try-catch 处于活动状态时有效的代码。中止事务在每个先前成功的请求上触发 req.onerror,每个请求又触发 transaction.onerror 和太多递归错误。我解决了这个问题,虽然了解不多,但我认为一切正常,以至于个别请求错误触发 req.onerror 并回滚事务。但是,似乎只是 catch 中的 abort 语句引发了这种情况。我通过发送无法解析为 JSON 字符串的错误数据来测试错误场景。

但是,现在,我在不需要 try-catch 和随后的中止的情况下使用相同的代码,我无法触发 req.onerror 并且不明白为什么。

传递给 DB_pop 的参数 o 是一个对象,其中包含对象存储的名称、数据对象数组以及 'add' 或 'put' 的字符串。我特意传递了一个良好的数据对象和一个带有空键的数据对象,如下面的第一个代码语句所示。好的被写了,坏的没有。 req.onerror 和 transaction.onerror 不会触发。只有 transaction.oncomplete 触发。此外,只有一个 req.onsuccess 触发。我得出这个结论是因为控制台日志中写入的内容。

then 语句连接到调用 DB_pop 函数的承诺,运行拒绝函数。回滚函数运行,并运行清理变量以防止内存泄漏的关闭 then。我也不明白这一点,因为如果只触发 transaction.oncomplete,它应该解决而不是拒绝。

为什么 req.onerror 不触发第二个数据对象并导致事务回滚并删除写入的第一个数据对象?

我试着让 req 成为一个数组,并删除了 d 循环并传递了一个带有 null 键的数据对象,但都没有触发 onerror 事件。

我确实注意到,如果在现有密钥上将错误编造为 add 到数据库,那么所有错误事件都会触发,甚至包括在数据库中设置的一般错误事件 open/create。似乎 put 中的空键不会按预期触发错误和回滚。

p = DB_pop( { 'os' : 'topics', 'data' : [ { 'key' : 0, 'gap' : T.gap, 'max' : T.max, 'selKey' : key }, { 'key' : null, 'title' : t, 'created' : d, 'edited' : d } ], 'op' : 'put' } ); 

p.then( () => { T.title = t; }, rollback ).then( () => {  evt = key = t = T = d = p = m = null; console.log('cleaned up'); } );




  function DB_pop( o )  // ( os, data, op )
    { 
      return new Promise( ( resolve, reject ) =>
        {
          if ( !DB_open.base ) reject( 'Failed to populate database ' + DB_open.title + ' because the database is closed or does not exist.' );

          let t = DB_open.base.transaction( [ o.os ], 'readwrite' ),
              s = t.objectStore( o.os ),
              req, d;

          DB_pop.error = false;

          function free_RAM() 
           { 
             t = s = req = d = null;
           } // close free_RAM

             t.oncomplete = ( e ) =>
               {
                 console.log('trans complete');
                 resolve();
                 free_RAM();
                 e = null;
               }; // close t.oncomplete

             t.onerror = ( e ) =>
               {
                 //e.stopPropagation(); // Stop propagation up to the database error level in the database open block.
                 DB_pop.error = true;
                 reject(e);
                 console.log( 'Transaction error : ' + e.target.error );
                 free_RAM();
                 e = null;
               }; // close t.onerror

             t.onabort = ( e ) =>
               {
                 console.log( 'Transaction aborted : ' + e.target.error );
                 free_RAM();
                 e = null;
               }; // close t.onabort


            for ( d of o.data )
              { 

               // try
               //   { let q = JSON.parse( { 'x' : d, 'y' : 3 }   ); }
               // catch(e)
                //  { t.abort(); error = true; break; }  //?????????? Test what takes place if the try fails.

                req = s[ o.op ]( d );  // o.op is either 'add' or 'put'.
                req.k = d.key; 
                req.n = d.nbr;

                req.onsuccess = function( e )
                  {
                    if ( !DB_pop.error )
                      {
                        console.log( 'Success at k = ' + this.k  + '.' );
                      }; // end if
                  }; // close req.onsuccess

                req.onerror = function( e ) 
                  { 
                    /*
                       When a transaction is rolled back/aborted, every request's error event is fired and propagates up to transaction.onerror and causes a "too much recursion" error.
                       Thus, let it propagate once and, within transaction.onerror, set error to true.
                    */

                    console.log( 'Request.onerror fired. Error = ' + e.target.error + ' k = ' + this.k + ', n = ' + this.n );

                    if ( DB_pop.error )
                      {
                        e.stopPropagation();
                        console.log( 'Stopping propagation' );
                      }; // end if
                  }; // close req.onerror

              }; // next d

        }); //  close promise

    } // close DB_pop

我的第一个猜测是因为你在循环中定义了一个函数。关键问题是这种模式:

for(...) {
  request.onsuccess = function() {
    //...
  };
}

有多种在循环中定义函数的有效方法,但上面的示例不是一种。在循环中定义一个函数通常是一件麻烦事,所以按照惯例我建议完全避免它。试试这个模式:

function outerfunction() {
   function innerhelperfunction(){}

   for(...) {
     request.onsuccess = innerhelperfunction;
   }
}

参见,例如,Javascript: Creating Functions in a For Loop

IDBObjectStore.put throws a DataError 因为提供的密钥无效。这将退出该函数并阻止创建请求(或执行 - 因此永远不会调用请求回调)。

事务正常完成,因为之前执行的所有数据库操作都成功了。 promise 本身失败了,因为该函数在试图创建无效请求的行抛出异常:

req = s[ o.op ]( d );

您可以在您的承诺的错误处理程序中显式调用 abort 或在您的处理函数中捕获异常。