Skip to content

Commit

Permalink
Handle drain exceptions in one place only (#71)
Browse files Browse the repository at this point in the history
* throw in drain so that we catch in advance

* test should check for closing prop

* no uncaught error in upgrade test

* disable autoack for all upgrade tests

* autobase append after close throws

* rely on replicateAndSync rejecting

* fix borked test, bases should close immediately on error

* _onUpgrade is only a side effect
  • Loading branch information
chm-diederichs authored Mar 28, 2024
1 parent 16b3ae7 commit 2a371bf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 81 deletions.
20 changes: 7 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ module.exports = class Autobase extends ReadyResource {

async append (value) {
if (!this.opened) await this.ready()

if (this.closing) throw new Error('Autobase is closing')

await this._advanced // ensure all local state has been applied, only needed until persistent batches

// if a reset is scheduled await those
Expand Down Expand Up @@ -759,11 +762,7 @@ module.exports = class Autobase extends ReadyResource {
}

_onUpgrade (version) {
if (version > this.maxSupportedVersion) {
this._onError(new Error('Autobase upgrade required'))
return false
}
return true
if (version > this.maxSupportedVersion) throw new Error('Autobase upgrade required')
}

_addLocalHeads () {
Expand Down Expand Up @@ -1436,7 +1435,7 @@ module.exports = class Autobase extends ReadyResource {
// autobase version was bumped
let upgraded = false
if (update.version > this.version) {
if (!this._onUpgrade(update.version)) return // failed
this._onUpgrade(update.version) // throws if not supported
upgraded = true
}

Expand Down Expand Up @@ -1501,12 +1500,7 @@ module.exports = class Autobase extends ReadyResource {
if (this.system.bootstrapping) await this._bootstrap()

if (applyBatch.length && this._hasApply === true) {
try {
await this._handlers.apply(applyBatch, this.view, this)
} catch (err) {
this._onError(err)
return null
}
await this._handlers.apply(applyBatch, this.view, this)
}

update.indexers = !!this.system.indexerUpdate
Expand All @@ -1531,7 +1525,7 @@ module.exports = class Autobase extends ReadyResource {
// autobase version was bumped
let upgraded = false
if (update.version > this.version) {
if (!this._onUpgrade(update.version)) return // failed
this._onUpgrade(update.version) // throws if not supported
upgraded = true
}

Expand Down
Loading

0 comments on commit 2a371bf

Please sign in to comment.