Skip to content

Commit 0b601b4

Browse files
committed
chore: remove use of CodeError
1 parent a4689b9 commit 0b601b4

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

packages/libp2p-daemon-client/src/dht.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
DHTRequest,
55
DHTResponse
66
} from '@libp2p/daemon-protocol'
7-
import { CodeError } from '@libp2p/interface'
7+
import { InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface'
88
import { isPeerId, type PeerId, type PeerInfo } from '@libp2p/interface'
99
import { logger } from '@libp2p/logger'
1010
import { peerIdFromMultihash } from '@libp2p/peer-id'
1111
import { multiaddr } from '@multiformats/multiaddr'
1212
import { CID } from 'multiformats/cid'
1313
import * as Digest from 'multiformats/hashes/digest'
14-
import type { DaemonClient } from './index.js'
14+
import { OperationFailedError, type DaemonClient } from './index.js'
1515

1616
const log = logger('libp2p:daemon-client:dht')
1717

@@ -27,11 +27,11 @@ export class DHT {
2727
*/
2828
async put (key: Uint8Array, value: Uint8Array): Promise<void> {
2929
if (!(key instanceof Uint8Array)) {
30-
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
30+
throw new InvalidParametersError('invalid key received')
3131
}
3232

3333
if (!(value instanceof Uint8Array)) {
34-
throw new CodeError('value received is not a Uint8Array', 'ERR_INVALID_VALUE')
34+
throw new InvalidParametersError('value received is not a Uint8Array')
3535
}
3636

3737
const sh = await this.client.send({
@@ -50,7 +50,7 @@ export class DHT {
5050
await sh.unwrap().close()
5151

5252
if (response.type !== Response.Type.OK) {
53-
throw new CodeError(response.error?.msg ?? 'DHT put failed', 'ERR_DHT_PUT_FAILED')
53+
throw new ProtocolError(response.error?.msg ?? 'DHT put failed')
5454
}
5555
}
5656

@@ -59,7 +59,7 @@ export class DHT {
5959
*/
6060
async get (key: Uint8Array): Promise<Uint8Array> {
6161
if (!(key instanceof Uint8Array)) {
62-
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
62+
throw new InvalidParametersError('invalid key received')
6363
}
6464

6565
const sh = await this.client.send({
@@ -75,11 +75,11 @@ export class DHT {
7575
await sh.unwrap().close()
7676

7777
if (response.type !== Response.Type.OK) {
78-
throw new CodeError(response.error?.msg ?? 'DHT get failed', 'ERR_DHT_GET_FAILED')
78+
throw new OperationFailedError(response.error?.msg ?? 'DHT get failed')
7979
}
8080

8181
if (response.dht?.value == null) {
82-
throw new CodeError('Invalid DHT get response', 'ERR_DHT_GET_FAILED')
82+
throw new OperationFailedError('Invalid DHT get response')
8383
}
8484

8585
return response.dht.value
@@ -90,7 +90,7 @@ export class DHT {
9090
*/
9191
async findPeer (peerId: PeerId): Promise<PeerInfo> {
9292
if (!isPeerId(peerId)) {
93-
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
93+
throw new InvalidParametersError('invalid peer id received')
9494
}
9595

9696
const sh = await this.client.send({
@@ -106,11 +106,11 @@ export class DHT {
106106
await sh.unwrap().close()
107107

108108
if (response.type !== Response.Type.OK) {
109-
throw new CodeError(response.error?.msg ?? 'DHT find peer failed', 'ERR_DHT_FIND_PEER_FAILED')
109+
throw new OperationFailedError(response.error?.msg ?? 'DHT find peer failed')
110110
}
111111

112112
if (response.dht?.peer?.addrs == null) {
113-
throw new CodeError('Invalid response', 'ERR_DHT_FIND_PEER_FAILED')
113+
throw new OperationFailedError('Invalid response')
114114
}
115115

116116
return {
@@ -124,7 +124,7 @@ export class DHT {
124124
*/
125125
async provide (cid: CID): Promise<void> {
126126
if (cid == null || CID.asCID(cid) == null) {
127-
throw new CodeError('invalid cid received', 'ERR_INVALID_CID')
127+
throw new InvalidParametersError('invalid cid received')
128128
}
129129

130130
const sh = await this.client.send({
@@ -140,7 +140,7 @@ export class DHT {
140140
await sh.unwrap().close()
141141

142142
if (response.type !== Response.Type.OK) {
143-
throw new CodeError(response.error?.msg ?? 'DHT provide failed', 'ERR_DHT_PROVIDE_FAILED')
143+
throw new OperationFailedError(response.error?.msg ?? 'DHT provide failed')
144144
}
145145
}
146146

@@ -149,7 +149,7 @@ export class DHT {
149149
*/
150150
async * findProviders (cid: CID, count: number = 1): AsyncIterable<PeerInfo> {
151151
if (cid == null || CID.asCID(cid) == null) {
152-
throw new CodeError('invalid cid received', 'ERR_INVALID_CID')
152+
throw new InvalidParametersError('invalid cid received')
153153
}
154154

155155
const sh = await this.client.send({
@@ -166,7 +166,7 @@ export class DHT {
166166

167167
if (response.type !== Response.Type.OK) {
168168
await sh.unwrap().close()
169-
throw new CodeError(response.error?.msg ?? 'DHT find providers failed', 'ERR_DHT_FIND_PROVIDERS_FAILED')
169+
throw new OperationFailedError(response.error?.msg ?? 'DHT find providers failed')
170170
}
171171

172172
while (true) {
@@ -187,7 +187,7 @@ export class DHT {
187187
} else {
188188
// Unexpected message received
189189
await sh.unwrap().close()
190-
throw new CodeError('unexpected message received', 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
190+
throw new ProtocolError('unexpected message received')
191191
}
192192
}
193193
}
@@ -197,7 +197,7 @@ export class DHT {
197197
*/
198198
async * getClosestPeers (key: Uint8Array): AsyncIterable<PeerInfo> {
199199
if (!(key instanceof Uint8Array)) {
200-
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
200+
throw new InvalidParametersError('invalid key received')
201201
}
202202

203203
const sh = await this.client.send({
@@ -213,7 +213,7 @@ export class DHT {
213213

214214
if (response.type !== Response.Type.OK) {
215215
await sh.unwrap().close()
216-
throw new CodeError(response.error?.msg ?? 'DHT find providers failed', 'ERR_DHT_FIND_PROVIDERS_FAILED')
216+
throw new OperationFailedError(response.error?.msg ?? 'DHT find providers failed')
217217
}
218218

219219
while (true) {
@@ -236,7 +236,7 @@ export class DHT {
236236
} else {
237237
// Unexpected message received
238238
await sh.unwrap().close()
239-
throw new CodeError('unexpected message received', 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
239+
throw new InvalidMessageError('unexpected message received')
240240
}
241241
}
242242
}
@@ -246,7 +246,7 @@ export class DHT {
246246
*/
247247
async getPublicKey (peerId: PeerId): Promise<Uint8Array | undefined> {
248248
if (!isPeerId(peerId)) {
249-
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
249+
throw new InvalidParametersError('invalid peer id received')
250250
}
251251

252252
const sh = await this.client.send({
@@ -262,11 +262,11 @@ export class DHT {
262262
await sh.unwrap().close()
263263

264264
if (response.type !== Response.Type.OK) {
265-
throw new CodeError(response.error?.msg ?? 'DHT get public key failed', 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
265+
throw new OperationFailedError(response.error?.msg ?? 'DHT get public key failed')
266266
}
267267

268268
if (response.dht == null) {
269-
throw new CodeError('Invalid response', 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
269+
throw new InvalidMessageError('Invalid response')
270270
}
271271

272272
return response.dht.value

packages/libp2p-daemon-client/src/index.ts

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type PSMessage, Request, Response, StreamInfo } from '@libp2p/daemon-protocol'
22
import { StreamHandler } from '@libp2p/daemon-protocol/stream-handler'
33
import { passThroughUpgrader } from '@libp2p/daemon-protocol/upgrader'
4-
import { CodeError, isPeerId } from '@libp2p/interface'
4+
import { InvalidParametersError, isPeerId } from '@libp2p/interface'
55
import { defaultLogger, logger } from '@libp2p/logger'
66
import { peerIdFromMultihash } from '@libp2p/peer-id'
77
import { tcp } from '@libp2p/tcp'
@@ -16,6 +16,13 @@ import type { CID } from 'multiformats/cid'
1616

1717
const log = logger('libp2p:daemon-client')
1818

19+
export class OperationFailedError extends Error {
20+
constructor (message = 'Operation failed') {
21+
super(message)
22+
this.name = 'OperationFailedError'
23+
}
24+
}
25+
1926
class Client implements DaemonClient {
2027
private readonly multiaddr: Multiaddr
2128
public dht: DHT
@@ -67,16 +74,16 @@ class Client implements DaemonClient {
6774
*/
6875
async connect (peerId: PeerId, addrs: Multiaddr[]): Promise<void> {
6976
if (!isPeerId(peerId)) {
70-
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
77+
throw new InvalidParametersError('invalid peer id received')
7178
}
7279

7380
if (!Array.isArray(addrs)) {
74-
throw new CodeError('addrs received are not in an array', 'ERR_INVALID_ADDRS_TYPE')
81+
throw new InvalidParametersError('addrs received are not in an array')
7582
}
7683

7784
addrs.forEach((addr) => {
7885
if (!isMultiaddr(addr)) {
79-
throw new CodeError('received an address that is not a multiaddr', 'ERR_NO_MULTIADDR_RECEIVED')
86+
throw new InvalidParametersError('received an address that is not a multiaddr')
8087
}
8188
})
8289

@@ -92,7 +99,7 @@ class Client implements DaemonClient {
9299

93100
if (response.type !== Response.Type.OK) {
94101
const errResponse = response.error ?? { msg: 'unspecified' }
95-
throw new CodeError(errResponse.msg ?? 'unspecified', 'ERR_CONNECT_FAILED')
102+
throw new OperationFailedError(errResponse.msg ?? 'unspecified')
96103
}
97104

98105
await sh.unwrap().close()
@@ -115,11 +122,11 @@ class Client implements DaemonClient {
115122
const response = await sh.read(Response)
116123

117124
if (response.type !== Response.Type.OK) {
118-
throw new CodeError(response.error?.msg ?? 'Identify failed', 'ERR_IDENTIFY_FAILED')
125+
throw new OperationFailedError(response.error?.msg ?? 'Identify failed')
119126
}
120127

121128
if (response.identify?.addrs == null) {
122-
throw new CodeError('Invalid response', 'ERR_IDENTIFY_FAILED')
129+
throw new OperationFailedError('Invalid response')
123130
}
124131

125132
const peerId = peerIdFromMultihash(Digest.decode(response.identify?.id))
@@ -141,7 +148,7 @@ class Client implements DaemonClient {
141148
const response = await sh.read(Response)
142149

143150
if (response.type !== Response.Type.OK) {
144-
throw new CodeError(response.error?.msg ?? 'List peers failed', 'ERR_LIST_PEERS_FAILED')
151+
throw new OperationFailedError(response.error?.msg ?? 'List peers failed')
145152
}
146153

147154
await sh.unwrap().close()
@@ -154,11 +161,11 @@ class Client implements DaemonClient {
154161
*/
155162
async openStream (peerId: PeerId, protocol: string): Promise<MultiaddrConnection> {
156163
if (!isPeerId(peerId)) {
157-
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
164+
throw new InvalidParametersError('invalid peer id received')
158165
}
159166

160167
if (typeof protocol !== 'string') {
161-
throw new CodeError('invalid protocol received', 'ERR_INVALID_PROTOCOL')
168+
throw new InvalidParametersError('invalid protocol received')
162169
}
163170

164171
const sh = await this.send({
@@ -173,7 +180,7 @@ class Client implements DaemonClient {
173180

174181
if (response.type !== Response.Type.OK) {
175182
await sh.unwrap().close()
176-
throw new CodeError(response.error?.msg ?? 'Open stream failed', 'ERR_OPEN_STREAM_FAILED')
183+
throw new OperationFailedError(response.error?.msg ?? 'Open stream failed')
177184
}
178185

179186
return sh.unwrap()
@@ -184,7 +191,7 @@ class Client implements DaemonClient {
184191
*/
185192
async registerStreamHandler (protocol: string, handler: StreamHandlerFunction): Promise<void> {
186193
if (typeof protocol !== 'string') {
187-
throw new CodeError('invalid protocol received', 'ERR_INVALID_PROTOCOL')
194+
throw new InvalidParametersError('invalid protocol received')
188195
}
189196

190197
// open a tcp port, pipe any data from it to the handler function
@@ -200,13 +207,13 @@ class Client implements DaemonClient {
200207
const message = await sh.read()
201208

202209
if (message == null) {
203-
throw new CodeError('Could not read open stream response', 'ERR_OPEN_STREAM_FAILED')
210+
throw new OperationFailedError('Could not read open stream response')
204211
}
205212

206213
const response = StreamInfo.decode(message)
207214

208215
if (response.proto !== protocol) {
209-
throw new CodeError('Incorrect protocol', 'ERR_OPEN_STREAM_FAILED')
216+
throw new OperationFailedError('Incorrect protocol')
210217
}
211218

212219
// @ts-expect-error because we are using a passthrough upgrader, this is a MultiaddrConnection
@@ -231,7 +238,7 @@ class Client implements DaemonClient {
231238
const address = listener.getAddrs()[0]
232239

233240
if (address == null) {
234-
throw new CodeError('Could not listen on port', 'ERR_REGISTER_STREAM_HANDLER_FAILED')
241+
throw new OperationFailedError('Could not listen on port')
235242
}
236243

237244
const sh = await this.send({
@@ -247,7 +254,7 @@ class Client implements DaemonClient {
247254
await sh.unwrap().close()
248255

249256
if (response.type !== Response.Type.OK) {
250-
throw new CodeError(response.error?.msg ?? 'Register stream handler failed', 'ERR_REGISTER_STREAM_HANDLER_FAILED')
257+
throw new OperationFailedError(response.error?.msg ?? 'Register stream handler failed')
251258
}
252259
}
253260
}

0 commit comments

Comments
 (0)