Skip to content

Commit adf2345

Browse files
authored
chore: specify raised exceptions in miscellaneous places (#1269)
1 parent f7daad9 commit adf2345

File tree

9 files changed

+81
-37
lines changed

9 files changed

+81
-37
lines changed

libp2p/connmanager.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ proc selectMuxer*(c: ConnManager, peerId: PeerId): Muxer =
276276
trace "connection not found", peerId
277277
return mux
278278

279-
proc storeMuxer*(c: ConnManager, muxer: Muxer) {.raises: [CatchableError].} =
279+
proc storeMuxer*(c: ConnManager, muxer: Muxer) {.raises: [LPError].} =
280280
## store the connection and muxer
281281
##
282282

libp2p/daemon/daemonapi.nim

+1
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ proc openStream*(
972972
raise newException(DaemonLocalError, "Wrong message type!")
973973

974974
proc streamHandler(server: StreamServer, transp: StreamTransport) {.async.} =
975+
# must not specify raised exceptions as this is StreamCallback from chronos
975976
var api = getUserData[DaemonAPI](server)
976977
var message = await transp.recvMessage()
977978
var pb = initProtoBuffer(message)

libp2p/discovery/discoverymngr.nim

+17-7
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,20 @@ type
7979
advertisementUpdated*: AsyncEvent
8080
advertiseLoop*: Future[void]
8181

82-
method request*(self: DiscoveryInterface, pa: PeerAttributes) {.async, base.} =
82+
DiscoveryError* = object of LPError
83+
DiscoveryFinished* = object of LPError
84+
85+
method request*(
86+
self: DiscoveryInterface, pa: PeerAttributes
87+
) {.base, async: (raises: [DiscoveryError, CancelledError]).} =
8388
doAssert(false, "Not implemented!")
8489

85-
method advertise*(self: DiscoveryInterface) {.async, base.} =
90+
method advertise*(
91+
self: DiscoveryInterface
92+
) {.base, async: (raises: [CancelledError]).} =
8693
doAssert(false, "Not implemented!")
8794

8895
type
89-
DiscoveryError* = object of LPError
90-
DiscoveryFinished* = object of LPError
91-
9296
DiscoveryQuery* = ref object
9397
attr: PeerAttributes
9498
peers: AsyncQueue[PeerAttributes]
@@ -137,7 +141,9 @@ template forEach*(query: DiscoveryQuery, code: untyped) =
137141
## peer attritubtes are available through the variable
138142
## `peer`
139143

140-
proc forEachInternal(q: DiscoveryQuery) {.async.} =
144+
proc forEachInternal(
145+
q: DiscoveryQuery
146+
) {.async: (raises: [CancelledError, DiscoveryError]).} =
141147
while true:
142148
let peer {.inject.} =
143149
try:
@@ -162,7 +168,11 @@ proc stop*(dm: DiscoveryManager) =
162168
continue
163169
i.advertiseLoop.cancel()
164170

165-
proc getPeer*(query: DiscoveryQuery): Future[PeerAttributes] {.async.} =
171+
proc getPeer*(
172+
query: DiscoveryQuery
173+
): Future[PeerAttributes] {.
174+
async: (raises: [CancelledError, DiscoveryError, DiscoveryFinished])
175+
.} =
166176
let getter = query.peers.popFirst()
167177

168178
try:

libp2p/discovery/rendezvousinterface.nim

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ type
2323

2424
proc `==`*(a, b: RdvNamespace): bool {.borrow.}
2525

26-
method request*(self: RendezVousInterface, pa: PeerAttributes) {.async.} =
26+
method request*(
27+
self: RendezVousInterface, pa: PeerAttributes
28+
) {.async: (raises: [DiscoveryError, CancelledError]).} =
2729
var namespace = ""
2830
for attr in pa:
2931
if attr.ofType(RdvNamespace):
@@ -48,7 +50,7 @@ method request*(self: RendezVousInterface, pa: PeerAttributes) {.async.} =
4850

4951
await sleepAsync(self.timeToRequest)
5052

51-
method advertise*(self: RendezVousInterface) {.async.} =
53+
method advertise*(self: RendezVousInterface) {.async: (raises: [CancelledError]).} =
5254
while true:
5355
var toAdvertise: seq[string]
5456
for attr in self.toAdvertise:

libp2p/peerstore.nim

+10-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,16 @@ proc cleanup*(peerStore: PeerStore, peerId: PeerId) =
188188
peerStore.del(peerStore.toClean[0])
189189
peerStore.toClean.delete(0)
190190

191-
proc identify*(peerStore: PeerStore, muxer: Muxer) {.async.} =
191+
proc identify*(
192+
peerStore: PeerStore, muxer: Muxer
193+
) {.
194+
async: (
195+
raises: [
196+
CancelledError, IdentityNoMatchError, IdentityInvalidMsgError, MultiStreamError,
197+
LPStreamError, MuxerError,
198+
]
199+
)
200+
.} =
192201
# new stream for identify
193202
var stream = await muxer.newStream()
194203
if stream == nil:

libp2p/protocols/connectivity/relay/client.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ proc handleRelayedConnect(
8080

8181
proc reserve*(
8282
cl: RelayClient, peerId: PeerId, addrs: seq[MultiAddress] = @[]
83-
): Future[Rsvp] {.async.} =
83+
): Future[Rsvp] {.async: (raises: [ReservationError, DialFailedError, CancelledError]).} =
8484
let conn = await cl.switch.dial(peerId, addrs, RelayV2HopCodec)
8585
defer:
8686
await conn.close()

libp2p/protocols/rendezvous.nim

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import
2020
../utils/heartbeat,
2121
../stream/connection,
2222
../utils/offsettedseq,
23-
../utils/semaphore
23+
../utils/semaphore,
24+
../discovery/discoverymngr
2425

2526
export chronicles
2627

@@ -295,7 +296,7 @@ proc decode(_: typedesc[Message], buf: seq[byte]): Opt[Message] =
295296
Opt.some(msg)
296297

297298
type
298-
RendezVousError* = object of LPError
299+
RendezVousError* = object of DiscoveryError
299300
RegisteredData = object
300301
expiration: Moment
301302
peerId: PeerId
@@ -555,7 +556,7 @@ proc requestLocally*(rdv: RendezVous, ns: string): seq[PeerRecord] =
555556

556557
proc request*(
557558
rdv: RendezVous, ns: string, l: int = DiscoverLimit.int, peers: seq[PeerId]
558-
): Future[seq[PeerRecord]] {.async.} =
559+
): Future[seq[PeerRecord]] {.async: (raises: [DiscoveryError, CancelledError]).} =
559560
var
560561
s: Table[PeerId, (PeerRecord, Register)]
561562
limit: uint64
@@ -634,7 +635,7 @@ proc request*(
634635

635636
proc request*(
636637
rdv: RendezVous, ns: string, l: int = DiscoverLimit.int
637-
): Future[seq[PeerRecord]] {.async.} =
638+
): Future[seq[PeerRecord]] {.async: (raises: [DiscoveryError, CancelledError]).} =
638639
await rdv.request(ns, l, rdv.peers)
639640

640641
proc unsubscribeLocally*(rdv: RendezVous, ns: string) =

libp2p/services/autorelayservice.nim

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ proc addressMapper(
4141

4242
proc reserveAndUpdate(
4343
self: AutoRelayService, relayPid: PeerId, switch: Switch
44-
) {.async.} =
44+
) {.async: (raises: [CatchableError]).} =
45+
# CatchableError used to simplify raised errors here, as there could be
46+
# many different errors raised but caller don't really care what is cause of error
4547
while self.running:
4648
let
4749
rsvp = await self.client.reserve(relayPid).wait(chronos.seconds(5))
@@ -86,7 +88,9 @@ method setup*(
8688
await self.run(switch)
8789
return hasBeenSetUp
8890

89-
proc manageBackedOff(self: AutoRelayService, pid: PeerId) {.async.} =
91+
proc manageBackedOff(
92+
self: AutoRelayService, pid: PeerId
93+
) {.async: (raises: [CancelledError]).} =
9094
await sleepAsync(chronos.seconds(5))
9195
self.backingOff.keepItIf(it != pid)
9296
self.peerAvailable.fire()

libp2p/switch.nim

+36-19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import
2727
protocols/secure/secure,
2828
peerinfo,
2929
utils/semaphore,
30+
./muxers/muxer,
3031
connmanager,
3132
nameresolving/nameresolver,
3233
peerid,
@@ -61,6 +62,8 @@ type
6162
started: bool
6263
services*: seq[Service]
6364

65+
UpgradeError* = object of LPError
66+
6467
Service* = ref object of RootObj
6568
inUse: bool
6669

@@ -216,30 +219,44 @@ proc mount*[T: LPProtocol](
216219
s.ms.addHandler(proto.codecs, proto, matcher)
217220
s.peerInfo.protocols.add(proto.codec)
218221

219-
proc upgrader(switch: Switch, trans: Transport, conn: Connection) {.async.} =
220-
let muxed = await trans.upgrade(conn, Opt.none(PeerId))
221-
switch.connManager.storeMuxer(muxed)
222-
await switch.peerStore.identify(muxed)
223-
await switch.connManager.triggerPeerEvents(
224-
muxed.connection.peerId, PeerEvent(kind: PeerEventKind.Identified, initiator: false)
225-
)
226-
trace "Connection upgrade succeeded"
222+
proc upgrader(
223+
switch: Switch, trans: Transport, conn: Connection
224+
) {.async: (raises: [CancelledError, UpgradeError]).} =
225+
try:
226+
let muxed = await trans.upgrade(conn, Opt.none(PeerId))
227+
switch.connManager.storeMuxer(muxed)
228+
await switch.peerStore.identify(muxed)
229+
await switch.connManager.triggerPeerEvents(
230+
muxed.connection.peerId,
231+
PeerEvent(kind: PeerEventKind.Identified, initiator: false),
232+
)
233+
except CancelledError as e:
234+
raise e
235+
except CatchableError as e:
236+
raise newException(UpgradeError, e.msg, e)
227237

228238
proc upgradeMonitor(
229239
switch: Switch, trans: Transport, conn: Connection, upgrades: AsyncSemaphore
230-
) {.async.} =
240+
) {.async: (raises: []).} =
241+
var upgradeSuccessful = false
231242
try:
232243
await switch.upgrader(trans, conn).wait(30.seconds)
233-
except CatchableError as exc:
234-
if exc isnot CancelledError:
235-
libp2p_failed_upgrades_incoming.inc()
236-
if not isNil(conn):
237-
await conn.close()
238-
trace "Exception awaiting connection upgrade", description = exc.msg, conn
244+
trace "Connection upgrade succeeded"
245+
upgradeSuccessful = true
246+
except CancelledError:
247+
trace "Connection upgrade cancelled", conn
248+
except AsyncTimeoutError:
249+
trace "Connection upgrade timeout", conn
250+
libp2p_failed_upgrades_incoming.inc()
251+
except UpgradeError as e:
252+
trace "Connection upgrade failed", description = e.msg, conn
253+
libp2p_failed_upgrades_incoming.inc()
239254
finally:
255+
if (not upgradeSuccessful) and (not isNil(conn)):
256+
await conn.close()
240257
upgrades.release()
241258

242-
proc accept(s: Switch, transport: Transport) {.async.} = # noraises
259+
proc accept(s: Switch, transport: Transport) {.async: (raises: []).} =
243260
## switch accept loop, ran for every transport
244261
##
245262

@@ -286,7 +303,7 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises
286303
await conn.close()
287304
return
288305

289-
proc stop*(s: Switch) {.async, public.} =
306+
proc stop*(s: Switch) {.public, async: (raises: [CancelledError]).} =
290307
## Stop listening on every transport, and
291308
## close every active connections
292309

@@ -318,7 +335,7 @@ proc stop*(s: Switch) {.async, public.} =
318335

319336
trace "Switch stopped"
320337

321-
proc start*(s: Switch) {.async, public.} =
338+
proc start*(s: Switch) {.public, async: (raises: [CancelledError, LPError]).} =
322339
## Start listening on every transport
323340

324341
if s.started:
@@ -340,7 +357,7 @@ proc start*(s: Switch) {.async, public.} =
340357
for fut in startFuts:
341358
if fut.failed:
342359
await s.stop()
343-
raise fut.error
360+
raise newException(LPError, "starting transports failed", fut.error)
344361

345362
for t in s.transports: # for each transport
346363
if t.addrs.len > 0 or t.running:

0 commit comments

Comments
 (0)