Skip to content

Commit a0bc8f9

Browse files
authored
fix: use named error instead of CodeError (#385)
`CodeError` is going away in the next libp2p release so use a named error instead.
1 parent 408ba5a commit a0bc8f9

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@
168168
"dependencies": {
169169
"@chainsafe/is-ip": "^2.0.1",
170170
"@chainsafe/netmask": "^2.0.0",
171-
"@libp2p/interface": "^1.0.0",
172171
"@multiformats/dns": "^1.0.3",
173172
"multiformats": "^13.0.0",
174173
"uint8-varint": "^2.0.1",

src/multiaddr.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* ```
1313
*/
1414

15-
import { CodeError } from '@libp2p/interface'
1615
import { base58btc } from 'multiformats/bases/base58'
1716
import { CID } from 'multiformats/cid'
1817
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
@@ -32,6 +31,13 @@ const DNS_CODES = [
3231
getProtocol('dnsaddr').code
3332
]
3433

34+
class NoAvailableResolverError extends Error {
35+
constructor (message = 'No available resolver') {
36+
super(message)
37+
this.name = 'NoAvailableResolverError'
38+
}
39+
}
40+
3541
/**
3642
* Creates a {@link Multiaddr} from a {@link MultiaddrInput}
3743
*/
@@ -232,7 +238,7 @@ export class Multiaddr implements MultiaddrInterface {
232238

233239
const resolver = resolvers.get(resolvableProto.name)
234240
if (resolver == null) {
235-
throw new CodeError(`no available resolver for ${resolvableProto.name}`, 'ERR_NO_AVAILABLE_RESOLVER')
241+
throw new NoAvailableResolverError(`no available resolver for ${resolvableProto.name}`)
236242
}
237243

238244
const result = await resolver(this, options)

src/resolvers/dnsaddr.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CodeError } from '@libp2p/interface'
21
import { dns, RecordType } from '@multiformats/dns'
32
import { multiaddr } from '../index.js'
43
import { getProtocol } from '../protocols-table.js'
@@ -9,6 +8,13 @@ import type { DNS } from '@multiformats/dns'
98
const MAX_RECURSIVE_DEPTH = 32
109
const { code: dnsaddrCode } = getProtocol('dnsaddr')
1110

11+
class RecursionLimitError extends Error {
12+
constructor (message = 'Max recursive depth reached') {
13+
super(message)
14+
this.name = 'RecursionLimitError'
15+
}
16+
}
17+
1218
export interface DNSADDROptions extends AbortOptions {
1319
/**
1420
* An optional DNS resolver
@@ -28,7 +34,7 @@ export const dnsaddrResolver: Resolver<DNSADDROptions> = async function dnsaddrR
2834
const recursionLimit = options.maxRecursiveDepth ?? MAX_RECURSIVE_DEPTH
2935

3036
if (recursionLimit === 0) {
31-
throw new CodeError('Max recursive depth reached', 'ERR_MAX_RECURSIVE_DEPTH_REACHED')
37+
throw new RecursionLimitError('Max recursive depth reached')
3238
}
3339

3440
const [, hostname] = ma.stringTuples().find(([proto]) => proto === dnsaddrCode) ?? []

test/resolvers.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('multiaddr resolve', () => {
8282

8383
// Resolve
8484
await expect(ma.resolve()).to.eventually.be.rejected()
85-
.and.to.have.property('code', 'ERR_NO_AVAILABLE_RESOLVER')
85+
.and.to.have.property('name', 'NoAvailableResolverError')
8686
})
8787

8888
describe('dnsaddr', () => {
@@ -153,7 +153,7 @@ describe('multiaddr resolve', () => {
153153
maxRecursiveDepth: 1
154154
})
155155

156-
await expect(resolvePromise).to.eventually.be.rejected().with.property('code', 'ERR_MAX_RECURSIVE_DEPTH_REACHED')
156+
await expect(resolvePromise).to.eventually.be.rejected().with.property('name', 'RecursionLimitError')
157157
})
158158

159159
it('should handle recursive loops', async () => {
@@ -165,7 +165,7 @@ describe('multiaddr resolve', () => {
165165
maxRecursiveDepth: 1
166166
})
167167

168-
await expect(resolvePromise).to.eventually.be.rejected().with.property('code', 'ERR_MAX_RECURSIVE_DEPTH_REACHED')
168+
await expect(resolvePromise).to.eventually.be.rejected().with.property('name', 'RecursionLimitError')
169169
})
170170

171171
it('should handle double quotes', async () => {

0 commit comments

Comments
 (0)