Skip to content

Commit 6a6d977

Browse files
committed
avoid pointless exception raising in dcutr/server
`dcutr/server` is the only protocol handler outside tests / examples that raises exceptions. The only caller of this handler is defined in `MultStreamSelect.handle`, which has a surrounding `except` block that catches `CatchableError`. When the handler raises a non-`CancelledError` the flow is: 1. `await protocolHolder.protocol.handler(conn, ms)` 2. The `finally` block after it 3. The `return`, both in successful and in exception case 4. The outer `except CatchableError as exc:`, it logs and discards `exc` 5. The outer `finally`: `await conn.close()` 6. `Stopped multistream handler` log, both in successful and exc case. By changing `dcutr/server` to only log but not also raise, difference is that the outer `except CatchableError` in step (4) is skipped. As that is a redundant log anyway (`dcutr/server` already logs), it should be fine to not raise. That's in line with all the other protocol handlers (besides tests / examples) and opens up limiting `{.raises.}` annotation for handlers to just `[CancelledError]`.
1 parent 08a48fa commit 6a6d977

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

libp2p/protocols/connectivity/dcutr/server.nim

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ proc new*(T: typedesc[Dcutr], switch: Switch, connectTimeout = 15.seconds, maxDi
6565
except CancelledError as err:
6666
raise err
6767
except AllFuturesFailedError as err:
68-
debug "Dcutr receiver could not connect to the remote peer, all connect attempts failed", peerDialableAddrs, msg = err.msg
69-
raise newException(DcutrError, "Dcutr receiver could not connect to the remote peer, all connect attempts failed", err)
68+
debug "Dcutr receiver could not connect to the remote peer, " &
69+
"all connect attempts failed", peerDialableAddrs, msg = err.msg
7070
except AsyncTimeoutError as err:
71-
debug "Dcutr receiver could not connect to the remote peer, all connect attempts timed out", peerDialableAddrs, msg = err.msg
72-
raise newException(DcutrError, "Dcutr receiver could not connect to the remote peer, all connect attempts timed out", err)
71+
debug "Dcutr receiver could not connect to the remote peer, " &
72+
"all connect attempts timed out", peerDialableAddrs, msg = err.msg
7373
except CatchableError as err:
74-
warn "Unexpected error when Dcutr receiver tried to connect to the remote peer", msg = err.msg
75-
raise newException(DcutrError, "Unexpected error when Dcutr receiver tried to connect to the remote peer", err)
74+
warn "Unexpected error when Dcutr receiver tried to connect " &
75+
"to the remote peer", msg = err.msg
7676

7777
let self = T()
7878
self.handler = handleStream

0 commit comments

Comments
 (0)