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

Commit bb4dc19

Browse files
Alan Shawdirkmc
andauthored
chore: update err-code dependency (#2287)
* chore: update err-code dependency `err-code` no longer takes a string as the first argument. This PR also removes a bunch of useless DEBUG logs that were logging just the error message and adds logging where a caught error is not directly passed back to callbacks. This should help with debuggability. License: MIT Signed-off-by: Alan Shaw <[email protected]> * fix: name tests License: MIT Signed-off-by: Alan Shaw <[email protected]> * refactor: republisher error msg Co-Authored-By: dirkmc <[email protected]> * refactor: republisher error msg Co-Authored-By: dirkmc <[email protected]>
1 parent a5b590c commit bb4dc19

File tree

11 files changed

+44
-121
lines changed

11 files changed

+44
-121
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"datastore-pubsub": "~0.1.1",
8383
"debug": "^4.1.0",
8484
"dlv": "^1.1.3",
85-
"err-code": "^1.1.2",
85+
"err-code": "^2.0.0",
8686
"file-type": "^12.0.1",
8787
"fnv1a": "^1.0.1",
8888
"fsm-event": "^2.1.0",

src/core/components/name-pubsub.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ const isNamePubsubEnabled = (node) => {
2121
// Get pubsub from IPNS routing
2222
const getPubsubRouting = (node) => {
2323
if (!node._ipns || !node._options.EXPERIMENTAL.ipnsPubsub) {
24-
const errMsg = 'IPNS pubsub subsystem is not enabled'
25-
26-
throw errcode(errMsg, 'ERR_IPNS_PUBSUB_NOT_ENABLED')
24+
throw errcode(new Error('IPNS pubsub subsystem is not enabled'), 'ERR_IPNS_PUBSUB_NOT_ENABLED')
2725
}
2826

2927
// Only one store and it is pubsub
@@ -35,9 +33,7 @@ const getPubsubRouting = (node) => {
3533
const pubsub = (node._ipns.routing.stores || []).find(s => IpnsPubsubDatastore.isIpnsPubsubDatastore(s))
3634

3735
if (!pubsub) {
38-
const errMsg = 'IPNS pubsub datastore not found'
39-
40-
throw errcode(errMsg, 'ERR_PUBSUB_DATASTORE_NOT_FOUND')
36+
throw errcode(new Error('IPNS pubsub datastore not found'), 'ERR_PUBSUB_DATASTORE_NOT_FOUND')
4137
}
4238

4339
return pubsub

src/core/components/name.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ module.exports = function name (self) {
9393
const key = options.key || 'self'
9494

9595
if (!self.isOnline()) {
96-
const errMsg = utils.OFFLINE_ERROR
97-
98-
log.error(errMsg)
99-
return callback(errcode(errMsg, 'OFFLINE_ERROR'))
96+
return callback(errcode(new Error(utils.OFFLINE_ERROR), 'OFFLINE_ERROR'))
10097
}
10198

10299
// TODO: params related logic should be in the core implementation
@@ -162,10 +159,7 @@ module.exports = function name (self) {
162159

163160
// TODO: params related logic should be in the core implementation
164161
if (offline && options.nocache) {
165-
const error = 'cannot specify both offline and nocache'
166-
167-
log.error(error)
168-
return callback(errcode(new Error(error), 'ERR_NOCACHE_AND_OFFLINE'))
162+
return callback(errcode(new Error('cannot specify both offline and nocache'), 'ERR_NOCACHE_AND_OFFLINE'))
169163
}
170164

171165
// Set node id as name for being resolved, if it is not received
@@ -187,17 +181,15 @@ module.exports = function name (self) {
187181
}
188182

189183
log.error(err)
190-
return callback(errcode(new Error('Invalid IPNS name.'), 'ERR_IPNS_INVALID_NAME'))
184+
return callback(errcode(new Error('Invalid IPNS name'), 'ERR_IPNS_INVALID_NAME'))
191185
}
192186

193187
// multihash is valid lets resolve with IPNS
194188
// IPNS resolve needs a online daemon
195189
if (!self.isOnline() && !offline) {
196-
const errMsg = utils.OFFLINE_ERROR
197-
198-
log.error(errMsg)
199-
return callback(errcode(errMsg, 'OFFLINE_ERROR'))
190+
return callback(errcode(new Error(utils.OFFLINE_ERROR), 'OFFLINE_ERROR'))
200191
}
192+
201193
self._ipns.resolve(`/${namespace}/${hash}`, options, appendRemainder(callback, remainder))
202194
}),
203195
pubsub: namePubsub(self)

src/core/ipns/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ class IPNS {
6464
// Resolve
6565
resolve (name, options, callback) {
6666
if (typeof name !== 'string') {
67-
const errMsg = `name received is not valid`
68-
69-
log.error(errMsg)
70-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_NAME'))
67+
return callback(errcode(new Error('name received is not valid'), 'ERR_INVALID_NAME'))
7168
}
7269

7370
if (typeof options === 'function') {

src/core/ipns/publisher.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ class IpnsPublisher {
2323
// publish record with a eol
2424
publishWithEOL (privKey, value, lifetime, callback) {
2525
if (!privKey || !privKey.bytes) {
26-
const errMsg = `one or more of the provided parameters are not defined`
27-
28-
log.error(errMsg)
29-
return callback(errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER'))
26+
return callback(errcode(new Error('invalid private key'), 'ERR_INVALID_PRIVATE_KEY'))
3027
}
3128

3229
PeerId.createFromPrivKey(privKey.bytes, (err, peerId) => {

src/core/ipns/republisher.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ class IpnsRepublisher {
2929

3030
start () {
3131
if (this._republishHandle) {
32-
const errMsg = 'already running'
33-
34-
log.error(errMsg)
35-
throw errcode(new Error(errMsg), 'ERR_REPUBLISH_ALREADY_RUNNING')
32+
throw errcode(new Error('republisher is already running'), 'ERR_REPUBLISH_ALREADY_RUNNING')
3633
}
3734

3835
// TODO: this handler should be isolated in another module
@@ -78,10 +75,7 @@ class IpnsRepublisher {
7875
const republishHandle = this._republishHandle
7976

8077
if (!republishHandle) {
81-
const errMsg = 'not running'
82-
83-
log.error(errMsg)
84-
return callback(errcode(new Error(errMsg), 'ERR_REPUBLISH_NOT_RUNNING'))
78+
return callback(errcode(new Error('republisher is not running'), 'ERR_REPUBLISH_NOT_RUNNING'))
8579
}
8680

8781
this._republishHandle = null
@@ -134,10 +128,7 @@ class IpnsRepublisher {
134128

135129
_republishEntry (privateKey, callback) {
136130
if (!privateKey || !privateKey.bytes) {
137-
const errMsg = `one or more of the provided parameters are not defined`
138-
139-
log.error(errMsg)
140-
return callback(errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER'))
131+
return callback(errcode(new Error('invalid private key'), 'ERR_INVALID_PRIVATE_KEY'))
141132
}
142133

143134
waterfall([
@@ -154,40 +145,29 @@ class IpnsRepublisher {
154145

155146
_getPreviousValue (peerId, callback) {
156147
if (!(PeerId.isPeerId(peerId))) {
157-
const errMsg = `peerId received is not valid`
158-
159-
log.error(errMsg)
160-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_PEER_ID'))
148+
return callback(errcode(new Error('invalid peer ID'), 'ERR_INVALID_PEER_ID'))
161149
}
162150

163151
this._datastore.get(ipns.getLocalKey(peerId.id), (err, dsVal) => {
164152
// error handling
165153
// no need to republish
166154
if (err && err.notFound) {
167-
const errMsg = `no previous entry for record with id: ${peerId.id}`
168-
169-
log.error(errMsg)
170-
return callback(errcode(new Error(errMsg), 'ERR_NO_ENTRY_FOUND'))
155+
return callback(errcode(new Error(`no previous entry for record with id: ${peerId.id}`), 'ERR_NO_ENTRY_FOUND'))
171156
} else if (err) {
172157
return callback(err)
173158
}
174159

175160
if (!Buffer.isBuffer(dsVal)) {
176-
const errMsg = `found ipns record that we couldn't process`
177-
178-
log.error(errMsg)
179-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_IPNS_RECORD'))
161+
return callback(errcode(new Error("found ipns record that we couldn't process"), 'ERR_INVALID_IPNS_RECORD'))
180162
}
181163

182164
// unmarshal data
183165
let record
184166
try {
185167
record = ipns.unmarshal(dsVal)
186168
} catch (err) {
187-
const errMsg = `found ipns record that we couldn't convert to a value`
188-
189-
log.error(errMsg)
190-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_IPNS_RECORD'))
169+
log.error(err)
170+
return callback(errcode(new Error(`found ipns record that we couldn't convert to a value`), 'ERR_INVALID_IPNS_RECORD'))
191171
}
192172

193173
callback(null, record.value)

src/core/ipns/resolver.js

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ class IpnsResolver {
2323
}
2424

2525
if (typeof name !== 'string') {
26-
const errMsg = `one or more of the provided parameters are not valid`
27-
28-
log.error(errMsg)
29-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_PARAMETER'))
26+
return callback(errcode(new Error('invalid name'), 'ERR_INVALID_NAME'))
3027
}
3128

3229
options = options || {}
@@ -35,10 +32,7 @@ class IpnsResolver {
3532
const nameSegments = name.split('/')
3633

3734
if (nameSegments.length !== 3 || nameSegments[0] !== '') {
38-
const errMsg = `invalid name syntax for ${name}`
39-
40-
log.error(errMsg)
41-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_NAME_SYNTAX'))
35+
return callback(errcode(new Error('invalid name'), 'ERR_INVALID_NAME'))
4236
}
4337

4438
const key = nameSegments[2]
@@ -101,27 +95,20 @@ class IpnsResolver {
10195

10296
this._routing.get(routingKey.toBuffer(), (err, record) => {
10397
if (err) {
98+
log.error(err)
10499
if (err.code !== 'ERR_NOT_FOUND') {
105-
const errMsg = `unexpected error getting the ipns record ${peerId.id}`
106-
107-
log.error(errMsg)
108-
return callback(errcode(new Error(errMsg), 'ERR_UNEXPECTED_ERROR_GETTING_RECORD'))
100+
return callback(errcode(new Error(`unexpected error getting the ipns record ${peerId.id}`), 'ERR_UNEXPECTED_ERROR_GETTING_RECORD'))
109101
}
110-
const errMsg = `record requested was not found for ${name} (${routingKey}) in the network`
111-
112-
log.error(errMsg)
113-
return callback(errcode(new Error(errMsg), 'ERR_NO_RECORD_FOUND'))
102+
return callback(errcode(new Error(`record requested was not found for ${name} (${routingKey}) in the network`), 'ERR_NO_RECORD_FOUND'))
114103
}
115104

116105
// IPNS entry
117106
let ipnsEntry
118107
try {
119108
ipnsEntry = ipns.unmarshal(record)
120109
} catch (err) {
121-
const errMsg = `found ipns record that we couldn't convert to a value`
122-
123-
log.error(errMsg)
124-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_RECORD_RECEIVED'))
110+
log.error(err)
111+
return callback(errcode(new Error(`found ipns record that we couldn't convert to a value`), 'ERR_INVALID_RECORD_RECEIVED'))
125112
}
126113

127114
// if the record has a public key validate it
@@ -132,26 +119,19 @@ class IpnsResolver {
132119
// Otherwise, try to get the public key from routing
133120
this._routing.get(routingKey.toBuffer(), (err, pubKey) => {
134121
if (err) {
122+
log.error(err)
135123
if (err.code !== 'ERR_NOT_FOUND') {
136-
const errMsg = `unexpected error getting the public key for the ipns record ${peerId.id}`
137-
138-
log.error(errMsg)
139-
return callback(errcode(new Error(errMsg), 'ERR_UNEXPECTED_ERROR_GETTING_PUB_KEY'))
124+
return callback(errcode(new Error(`unexpected error getting the public key for the ipns record ${peerId.id}`), 'ERR_UNEXPECTED_ERROR_GETTING_PUB_KEY'))
140125
}
141-
const errMsg = `public key requested was not found for ${name} (${routingPubKey}) in the network`
142-
143-
log.error(errMsg)
144-
return callback(errcode(new Error(errMsg), 'ERR_NO_RECORD_FOUND'))
126+
return callback(errcode(new Error(`public key requested was not found for ${name} (${routingPubKey}) in the network`), 'ERR_NO_RECORD_FOUND'))
145127
}
146128

147129
try {
148130
// Insert it into the peer id, in order to be validated by IPNS validator
149131
peerId.pubKey = crypto.keys.unmarshalPublicKey(pubKey)
150132
} catch (err) {
151-
const errMsg = `found public key record that we couldn't convert to a value`
152-
153-
log.error(errMsg)
154-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_PUB_KEY_RECEIVED'))
133+
log.error(err)
134+
return callback(errcode(new Error(`found public key record that we couldn't convert to a value`), 'ERR_INVALID_PUB_KEY_RECEIVED'))
155135
}
156136

157137
this._validateRecord(peerId, ipnsEntry, callback)

src/core/ipns/routing/offline-datastore.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,20 @@ class OfflineDatastore {
2525
*/
2626
put (key, value, callback) {
2727
if (!Buffer.isBuffer(key)) {
28-
const errMsg = `Offline datastore key must be a buffer`
29-
30-
log.error(errMsg)
31-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
28+
return callback(errcode(new Error('Offline datastore key must be a buffer'), 'ERR_INVALID_KEY'))
3229
}
3330

3431
if (!Buffer.isBuffer(value)) {
35-
const errMsg = `Offline datastore value must be a buffer`
36-
37-
log.error(errMsg)
38-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_VALUE'))
32+
return callback(errcode(new Error('Offline datastore value must be a buffer'), 'ERR_INVALID_VALUE'))
3933
}
4034

4135
let routingKey
4236

4337
try {
4438
routingKey = this._routingKey(key)
4539
} catch (err) {
46-
const errMsg = `Not possible to generate the routing key`
47-
48-
log.error(errMsg)
49-
return callback(errcode(new Error(errMsg), 'ERR_GENERATING_ROUTING_KEY'))
40+
log.error(err)
41+
return callback(errcode(new Error('Not possible to generate the routing key'), 'ERR_GENERATING_ROUTING_KEY'))
5042
}
5143

5244
// Marshal to libp2p record as the DHT does
@@ -63,21 +55,16 @@ class OfflineDatastore {
6355
*/
6456
get (key, callback) {
6557
if (!Buffer.isBuffer(key)) {
66-
const errMsg = `Offline datastore key must be a buffer`
67-
68-
log.error(errMsg)
69-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
58+
return callback(errcode(new Error('Offline datastore key must be a buffer'), 'ERR_INVALID_KEY'))
7059
}
7160

7261
let routingKey
7362

7463
try {
7564
routingKey = this._routingKey(key)
7665
} catch (err) {
77-
const errMsg = `Not possible to generate the routing key`
78-
79-
log.error(errMsg)
80-
return callback(errcode(new Error(errMsg), 'ERR_GENERATING_ROUTING_KEY'))
66+
log.error(err)
67+
return callback(errcode(new Error('Not possible to generate the routing key'), 'ERR_GENERATING_ROUTING_KEY'))
8168
}
8269

8370
this._repo.datastore.get(routingKey, (err, res) => {

src/core/ipns/routing/pubsub-datastore.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ class IpnsPubsubDatastore {
6969
const subscriber = this._subscriptions[key]
7070

7171
if (!subscriber) {
72-
const errMsg = `key ${key} does not correspond to a subscription`
73-
74-
log.error(errMsg)
75-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_KEY'))
72+
return callback(errcode(new Error(`key ${key} does not correspond to a subscription`), 'ERR_INVALID_KEY'))
7673
}
7774

7875
let keys
@@ -105,10 +102,7 @@ class IpnsPubsubDatastore {
105102
*/
106103
cancel (name, callback) {
107104
if (typeof name !== 'string') {
108-
const errMsg = `received subscription name is not valid`
109-
110-
log.error(errMsg)
111-
return callback(errcode(new Error(errMsg), 'ERR_INVALID_SUBSCRIPTION_NAME'))
105+
return callback(errcode(new Error('invalid subscription name'), 'ERR_INVALID_SUBSCRIPTION_NAME'))
112106
}
113107

114108
// Trim /ipns/ prefix from the name

src/core/runtime/dns-nodejs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = (domain, opts, callback) => {
2121

2222
function recursiveResolveDnslink (domain, depth, callback) {
2323
if (depth === 0) {
24-
return callback(errcode(`recursion limit exceeded`, 'ERR_DNSLINK_RECURSION_LIMIT'))
24+
return callback(errcode(new Error('recursion limit exceeded'), 'ERR_DNSLINK_RECURSION_LIMIT'))
2525
}
2626

2727
return resolveDnslink(domain)
@@ -70,7 +70,7 @@ function resolveDnslink (domain) {
7070
// we now have dns text entries as an array of strings
7171
// only records passing the DNSLINK_REGEX text are included
7272
if (dnslinkRecords.length === 0) {
73-
throw errcode(`No dnslink records found for domain: ${domain}`, 'ERR_DNSLINK_NOT_FOUND')
73+
throw errcode(new Error(`No dnslink records found for domain: ${domain}`), 'ERR_DNSLINK_NOT_FOUND')
7474
}
7575
return dnslinkRecords[0]
7676
})

0 commit comments

Comments
 (0)