You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dexie中useLiveQuery()内UpgradeError静默问题咨询

关于Dexie中useLiveQuery()静默错误的疑问

我此前并不知晓Dexie不支持修改主键,触发了错误:UpgradeError Not yet support for changing primary key。但核心问题是该错误在useLiveQuery()中处于静默状态:

useLiveQuery(
  () => db.tournaments.limit(5).reverse().toArray(),
  [],
  "loading",
);

useEffect(() => {
  window.addEventListener("unhandledrejection", (event) => {
    console.error(event); // 无错误事件触发,此代码无效
  });
}, []);

由于错误静默,我的自定义ErrorBoundary无法触发,除非在每个useLiveQuery()中手动捕获并重新抛出错误:

useLiveQuery(
  () => {
    try {
      return db.tournaments.limit(5).reverse().toArray()
    } catch(e) {
      throw new Error("DB error")
    }
  },
  [],
  "loading",
);

我排查后发现该错误可能被dexie.js第1274行的代码捕获处理:

Table.prototype._trans = function (mode, fn, writeLocked) {
  var trans = this._tx || PSD.trans;
  var tableName = this.name;
  var task = debug && typeof console !== 'undefined' && console.createTask && console.createTask("Dexie: ".concat(mode === 'readonly' ? 'read' : 'write', " ").concat(this.name));
  function checkTableInTransaction(resolve, reject, trans) {
      if (!trans.schema[tableName])
          throw new exceptions.NotFound("Table " + tableName + " not part of transaction");
      return fn(trans.idbtrans, trans);
  }
  var wasRootExec = beginMicroTickScope();
  try {
      var p = trans && trans.db._novip === this.db._novip ?
          trans === PSD.trans ?
              trans._promise(mode, checkTableInTransaction, writeLocked) :
              newScope(function () { return trans._promise(mode, checkTableInTransaction, writeLocked); }, { trans: trans, transless: PSD.transless || PSD }) :
          tempTransaction(this.db, mode, [this.name], checkTableInTransaction);
      if (task) {
          p._consoleTask = task;
          p = p.catch(function (err) {
              console.trace(err); <<<<===== 此处捕获错误
              return rejection(err);
          });
      }
      return p;
  } finally {
      if (wasRootExec)
          endMicroTickScope();
  }
};

请问这类错误(比如DatabaseClosedError)本就应该在useLiveQuery()中保持静默吗?


内容的提问来源于stack exchange,提问作者RedGiant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 17:37:32