Skip to content

Commit dece009

Browse files
committed
chore: replace custom error code fn with npm module
1 parent 65d80f6 commit dece009

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"base32-encode": "^1.1.0",
3737
"debug": "^4.1.1",
38+
"err-code": "^1.1.2",
3839
"interface-datastore": "~0.6.0",
3940
"left-pad": "^1.3.0",
4041
"libp2p-crypto": "libp2p/js-libp2p-crypto#feat/async-await",

src/index.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { Key } = require('interface-datastore')
66
const crypto = require('libp2p-crypto')
77
const PeerId = require('peer-id')
88
const multihash = require('multihashes')
9+
const errCode = require('err-code')
910

1011
const debug = require('debug')
1112
const log = debug('jsipns')
@@ -67,7 +68,7 @@ const _create = async (privateKey, value, seq, isoValidity, validityType) => {
6768
return entry
6869
} catch (error) {
6970
log.error('record signature creation failed')
70-
throw errorWithCode('record signature verification failed', ERRORS.ERR_SIGNATURE_CREATION)
71+
throw errCode('record signature verification failed', ERRORS.ERR_SIGNATURE_CREATION)
7172
}
7273
}
7374

@@ -90,7 +91,7 @@ const validate = async (publicKey, entry) => {
9091
}
9192
if (!isValid) {
9293
log.error('record signature verification failed')
93-
throw errorWithCode('record signature verification failed', ERRORS.ERR_SIGNATURE_VERIFICATION)
94+
throw errCode('record signature verification failed', ERRORS.ERR_SIGNATURE_VERIFICATION)
9495
}
9596

9697
// Validate according to the validity type
@@ -101,16 +102,16 @@ const validate = async (publicKey, entry) => {
101102
validityDate = parseRFC3339(validity.toString())
102103
} catch (e) {
103104
log.error('unrecognized validity format (not an rfc3339 format)')
104-
throw errorWithCode('unrecognized validity format (not an rfc3339 format)', ERRORS.ERR_UNRECOGNIZED_FORMAT)
105+
throw errCode('unrecognized validity format (not an rfc3339 format)', ERRORS.ERR_UNRECOGNIZED_FORMAT)
105106
}
106107

107108
if (validityDate < Date.now()) {
108109
log.error('record has expired')
109-
throw errorWithCode('record has expired', ERRORS.ERR_IPNS_EXPIRED_RECORD)
110+
throw errCode('record has expired', ERRORS.ERR_IPNS_EXPIRED_RECORD)
110111
}
111112
} else if (validityType) {
112113
log.error('unrecognized validity type')
113-
throw errorWithCode('unrecognized validity type', ERRORS.ERR_UNRECOGNIZED_VALIDITY)
114+
throw errCode('unrecognized validity type', ERRORS.ERR_UNRECOGNIZED_VALIDITY)
114115
}
115116

116117
log(`ipns entry for ${value} is valid`)
@@ -133,15 +134,15 @@ const embedPublicKey = async (publicKey, entry) => {
133134
if (!publicKey || !publicKey.bytes || !entry) {
134135
const error = 'one or more of the provided parameters are not defined'
135136
log.error(error)
136-
throw Object.assign(new Error(error), { code: ERRORS.ERR_UNDEFINED_PARAMETER })
137+
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
137138
}
138139

139140
// Create a peer id from the public key.
140141
let peerId
141142
try {
142143
peerId = await PeerId.createFromPubKey(publicKey.bytes)
143144
} catch (err) {
144-
throw Object.assign(new Error(err), { code: ERRORS.ERR_PEER_ID_FROM_PUBLIC_KEY })
145+
throw errCode(err, ERRORS.ERR_PEER_ID_FROM_PUBLIC_KEY)
145146
}
146147

147148
// Try to extract the public key from the ID. If we can, no need to embed it
@@ -150,7 +151,7 @@ const embedPublicKey = async (publicKey, entry) => {
150151
extractedPublicKey = extractPublicKeyFromId(peerId)
151152
} catch (err) {
152153
log.error(err)
153-
throw Object.assign(new Error(err), { code: ERRORS.ERR_PUBLIC_KEY_FROM_ID })
154+
throw errCode(err, ERRORS.ERR_PUBLIC_KEY_FROM_ID)
154155
}
155156

156157
if (extractedPublicKey) {
@@ -179,7 +180,7 @@ const extractPublicKey = (peerId, entry) => {
179180
const error = 'one or more of the provided parameters are not defined'
180181

181182
log.error(error)
182-
throw Object.assign(new Error(error), { code: ERRORS.ERR_UNDEFINED_PARAMETER })
183+
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
183184
}
184185

185186
if (entry.pubKey) {
@@ -240,7 +241,7 @@ const getValidityType = (validityType) => {
240241

241242
const error = `unrecognized validity type ${validityType.toString()}`
242243
log.error(error)
243-
throw Object.assign(new Error(error), { code: ERRORS.ERR_UNRECOGNIZED_VALIDITY })
244+
throw errCode(error, ERRORS.ERR_UNRECOGNIZED_VALIDITY)
244245
}
245246

246247
// Utility for creating the record data for being signed
@@ -263,8 +264,6 @@ const extractPublicKeyFromId = (peerId) => {
263264
return crypto.keys.unmarshalPublicKey(decodedId.digest)
264265
}
265266

266-
const errorWithCode = (err, code) => Object.assign(new Error(err), { code })
267-
268267
const marshal = ipnsEntryProto.encode
269268

270269
const unmarshal = ipnsEntryProto.decode

0 commit comments

Comments
 (0)