vacp2p / nim-libp2p

libp2p implementation in Nim
https://vacp2p.github.io/nim-libp2p/docs/
MIT License
242 stars 52 forks source link

add support for setting protocol handlers with `{.raises.}` annotation #1064

Closed etan-status closed 3 months ago

etan-status commented 3 months ago

All of the internal protocol handlers are restricted to {.raises.} of [CancelledError]. However, the LPProtoHandler type is part of the public interface, and example / test code raises more errors than just [CancelledError]. The caller is aware of that and CatchableError is caught.

To allow annotating the internal handlers with the proper {.raises.} annotation, support for an extra LPProtoHandler2 is added. This is backward compatible as the old LPProtoHandler is still allowed. Examples still compile fine. There is one test that needs a slight adjustment as it accesses the internal handler field directly. That field needs to be renamed so that the template is used instead.

Eventually, LPProtoHandler may be phased out, with appropriate notices to users who define custom handlers and the migration requiring errors to be handled inside the handler instead of raising them. At this time, such a deprecation is not yet applied, especially while the internal logic is still relying on the previous handler flow.

codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 82.56%. Comparing base (08a48fa) to head (417922c).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/status-im/nim-libp2p/pull/1064/graphs/tree.svg?width=650&height=150&src=pr&token=UR5JRQ249W&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=status-im)](https://app.codecov.io/gh/status-im/nim-libp2p/pull/1064?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=status-im) ```diff @@ Coverage Diff @@ ## unstable #1064 +/- ## ========================================= Coverage 82.56% 82.56% ========================================= Files 91 91 Lines 15814 15816 +2 ========================================= + Hits 13057 13059 +2 Misses 2757 2757 ``` | [Files](https://app.codecov.io/gh/status-im/nim-libp2p/pull/1064?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=status-im) | Coverage Δ | | |---|---|---| | [libp2p/multistream.nim](https://app.codecov.io/gh/status-im/nim-libp2p/pull/1064?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=status-im#diff-bGlicDJwL211bHRpc3RyZWFtLm5pbQ==) | `93.71% <ø> (ø)` | | | [libp2p/protocols/protocol.nim](https://app.codecov.io/gh/status-im/nim-libp2p/pull/1064?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=status-im#diff-bGlicDJwL3Byb3RvY29scy9wcm90b2NvbC5uaW0=) | `100.00% <100.00%> (ø)` | |