Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: emit boot error only once (#1472)
Browse files Browse the repository at this point in the history
`init` and `start` both emit any error they encounter. `boot` will then receive that error and also emit it! This PR adds an `emitted` property in `boot` to errors that came from `init` or `start` so that later in the code it knows whether to emit it or not!

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
alanshaw authored Jul 27, 2018
1 parent 1f63e8c commit 45b80a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ module.exports = (self) => {
(repoOpened, cb) => {
// Init with existing initialized, opened, repo
if (repoOpened) {
return self.init({ repo: self._repo }, (err) => cb(err))
return self.init({ repo: self._repo }, (err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}

if (doInit) {
const initOptions = Object.assign(
{ bits: 2048, pass: self._options.pass },
typeof options.init === 'object' ? options.init : {}
)
return self.init(initOptions, (err) => cb(err))
return self.init(initOptions, (err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}

cb()
Expand All @@ -48,11 +54,18 @@ module.exports = (self) => {
if (!doStart) {
return cb()
}
self.start(cb)

self.start((err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}
], (err) => {
if (err) {
return self.emit('error', err)
if (!err.emitted) {
self.emit('error', err)
}
return
}
self.log('booted')
self.emit('ready')
Expand Down
24 changes: 19 additions & 5 deletions test/core/create-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('create node', function () {
})
})

it('init: false errors (start default: true)', function (done) {
it('init: false errors (start default: true) and errors only once', function (done) {
this.timeout(80 * 1000)

const node = new IPFS({
Expand All @@ -135,10 +135,24 @@ describe('create node', function () {
}
}
})
node.once('error', (err) => {
expect(err).to.exist()
done()
})

const shouldHappenOnce = () => {
let timeoutId = null

return (err) => {
expect(err).to.exist()

// Bad news, this handler has been executed before
if (timeoutId) {
clearTimeout(timeoutId)
return done(new Error('error handler called multiple times'))
}

timeoutId = setTimeout(done, 100)
}
}

node.on('error', shouldHappenOnce())
})

it('init: false, start: false', function (done) {
Expand Down

0 comments on commit 45b80a0

Please sign in to comment.