Skip to content

Commit 8128ae0

Browse files
committed
feat: add err-code and cleanup logic
1 parent b84ba05 commit 8128ae0

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"async": "^2.6.1",
41+
"err-code": "^1.1.2",
4142
"joi": "^13.6.0",
4243
"joi-browser": "^13.4.0",
4344
"libp2p-connection-manager": "~0.0.2",

src/content-routing.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const tryEach = require('async/tryEach')
44
const parallel = require('async/parallel')
5+
const errCode = require('err-code')
56

67
module.exports = (node) => {
78
const routers = node._modules.contentRouting || []
@@ -23,14 +24,14 @@ module.exports = (node) => {
2324
* @returns {void}
2425
*/
2526
findProviders: (key, options, callback) => {
26-
if (routers.length === 0) {
27-
return callback(new Error('No content routers available'))
27+
if (!routers.length) {
28+
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
2829
}
2930

3031
if (typeof options === 'function') {
3132
callback = options
3233
options = {}
33-
} else if (typeof options === 'number') {
34+
} else if (typeof options === 'number') { // This can be deprecated in a future release
3435
options = {
3536
maxTimeout: options
3637
}
@@ -44,9 +45,7 @@ module.exports = (node) => {
4445

4546
// If we don't have any results, we need to provide an error to keep trying
4647
if (!results || Object.keys(results).length === 0) {
47-
return cb(Object.assign(new Error('not found'), {
48-
code: 'NOT_FOUND'
49-
}), null)
48+
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
5049
}
5150

5251
cb(null, results)
@@ -71,8 +70,8 @@ module.exports = (node) => {
7170
* @returns {void}
7271
*/
7372
provide: (key, callback) => {
74-
if (routers.length === 0) {
75-
return callback(new Error('No content routers available'))
73+
if (!routers.length) {
74+
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
7675
}
7776

7877
parallel(routers.map((router) => {

src/peer-routing.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const tryEach = require('async/tryEach')
4+
const errCode = require('err-code')
45

56
module.exports = (node) => {
67
const routers = node._modules.peerRouting || []
@@ -21,8 +22,8 @@ module.exports = (node) => {
2122
* @returns {void}
2223
*/
2324
findPeer: (id, options, callback) => {
24-
if (routers.length === 0) {
25-
return callback(new Error('No peer routers available'))
25+
if (!routers.length) {
26+
callback(errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE'))
2627
}
2728

2829
if (typeof options === 'function') {
@@ -38,9 +39,7 @@ module.exports = (node) => {
3839

3940
// If we don't have a result, we need to provide an error to keep trying
4041
if (!result || Object.keys(result).length === 0) {
41-
return cb(Object.assign(new Error('not found'), {
42-
code: 'NOT_FOUND'
43-
}), null)
42+
return cb(errCode(new Error('not found'), 'NOT_FOUND'), null)
4443
}
4544

4645
cb(null, result)
@@ -51,7 +50,7 @@ module.exports = (node) => {
5150
if (err && err.code !== 'NOT_FOUND') {
5251
return callback(err)
5352
}
54-
results = results || null
53+
results = results || []
5554
callback(null, results)
5655
})
5756
}

test/content-routing.node.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('.contentRouting', () => {
100100
})
101101

102102
it('nodeE.contentRouting.findProviders for existing record', (done) => {
103-
nodeE.contentRouting.findProviders(cid, 5000, (err, providers) => {
103+
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
104104
expect(err).to.not.exist()
105105
expect(providers).to.have.length.above(0)
106106
done()
@@ -110,7 +110,7 @@ describe('.contentRouting', () => {
110110
it('nodeC.contentRouting.findProviders for non existing record (timeout)', (done) => {
111111
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSnnnn')
112112

113-
nodeE.contentRouting.findProviders(cid, 5000, (err, providers) => {
113+
nodeE.contentRouting.findProviders(cid, { maxTimeout: 5000 }, (err, providers) => {
114114
expect(err).to.not.exist()
115115
expect(providers).to.have.length(0)
116116
done()
@@ -339,7 +339,7 @@ describe('.contentRouting', () => {
339339
return new Error('the delegate should not have been called')
340340
})
341341

342-
nodeA.contentRouting.findProviders('a cid', 5000, (err, results) => {
342+
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
343343
expect(err).to.not.exist()
344344
expect(results).to.equal(results)
345345
expect(dhtStub.calledOnce).to.equal(true)
@@ -355,7 +355,7 @@ describe('.contentRouting', () => {
355355
const dhtStub = sinon.stub(nodeA._dht, 'findProviders').callsArgWith(2, null, [])
356356
const delegateStub = sinon.stub(delegate, 'findProviders').callsArgWith(2, null, results)
357357

358-
nodeA.contentRouting.findProviders('a cid', 5000, (err, results) => {
358+
nodeA.contentRouting.findProviders('a cid', { maxTimeout: 5000 }, (err, results) => {
359359
expect(err).to.not.exist()
360360
expect(results).to.deep.equal(results)
361361
expect(dhtStub.calledOnce).to.equal(true)

0 commit comments

Comments
 (0)