Swap Defect additions for Errors, remove async raises

1. Swap TransportDefect for TransportError
2. Remove LPDefect, swap for LPError
3. Remove all `raises` pragmas from async procs as they are not yet supported.
This commit is contained in:
Eric Mastro 2022-01-19 11:21:31 +11:00
parent d4a65503ac
commit 9a79de0c62
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
9 changed files with 21 additions and 19 deletions

View File

@ -136,7 +136,7 @@ proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuil
b b
proc build*(b: SwitchBuilder): Switch proc build*(b: SwitchBuilder): Switch
{.raises: [Defect, LPDefect, LPError].} = {.raises: [Defect, LPError].} =
if b.rng == nil: # newRng could fail if b.rng == nil: # newRng could fail
raise newException(Defect, "Cannot initialize RNG") raise newException(Defect, "Cannot initialize RNG")
@ -210,7 +210,7 @@ proc newStandardSwitch*(
maxOut = -1, maxOut = -1,
maxConnsPerPeer = MaxConnectionsPerPeer, maxConnsPerPeer = MaxConnectionsPerPeer,
nameResolver: NameResolver = nil): Switch nameResolver: NameResolver = nil): Switch
{.raises: [Defect, LPDefect, LPError].} = {.raises: [Defect, LPError].} =
if SecureProtocol.Secio in secureManagers: if SecureProtocol.Secio in secureManagers:
quit("Secio is deprecated!") # use of secio is unsafe quit("Secio is deprecated!") # use of secio is unsafe

View File

@ -9,8 +9,6 @@ type
# Base exception type for libp2p # Base exception type for libp2p
LPError* = object of CatchableError LPError* = object of CatchableError
LPDefect* = object of CatchableError
func toException*(e: cstring): ref LPError = func toException*(e: cstring): ref LPError =
(ref LPError)(msg: $e) (ref LPError)(msg: $e)

View File

@ -235,7 +235,8 @@ proc stop*(s: Switch) {.async.} =
trace "Switch stopped" trace "Switch stopped"
proc start*(s: Switch) {.async, gcsafe, raises: [Defect, TransportListenError].} = # TODO: add {.raises: [TransportListenError].} when supported by chronos
proc start*(s: Switch) {.async, gcsafe.} =
trace "starting switch for peer", peerInfo = s.peerInfo trace "starting switch for peer", peerInfo = s.peerInfo
for t in s.transports: for t in s.transports:
let addrs = s.peerInfo.addrs.filterIt( let addrs = s.peerInfo.addrs.filterIt(
@ -261,13 +262,13 @@ proc newSwitch*(peerInfo: PeerInfo,
connManager: ConnManager, connManager: ConnManager,
ms: MultistreamSelect, ms: MultistreamSelect,
nameResolver: NameResolver = nil): Switch nameResolver: NameResolver = nil): Switch
{.raises: [Defect, LPDefect, LPError].} = {.raises: [Defect, LPError].} =
if transports.len == 0: if transports.len == 0:
raise newException(LPDefect, "Provide at least one transport") raise newException(LPError, "Provide at least one transport")
if peerInfo.addrs.len == 0: if peerInfo.addrs.len == 0:
raise newException(LPDefect, "Provide at least one address") raise newException(LPError, "Provide at least one address")
if secureManagers.len == 0: if secureManagers.len == 0:
raise newException(LPError, "Provide at least one secure manager") raise newException(LPError, "Provide at least one secure manager")

View File

@ -129,9 +129,10 @@ proc new*(
inc getTcpTransportTracker().opened inc getTcpTransportTracker().opened
return transport return transport
# TODO: add {.raises: [TransportListenError].} when supported by chronos
method start*( method start*(
self: TcpTransport, self: TcpTransport,
addrs: seq[MultiAddress]) {.async, raises: [Defect, TransportListenError].} = addrs: seq[MultiAddress]) {.async.} =
## listen on the transport ## listen on the transport
## ##

View File

@ -21,7 +21,6 @@ logScope:
topics = "libp2p transport" topics = "libp2p transport"
type type
TransportDefect* = object of Defect
ListenErrorCallback* = proc ( ListenErrorCallback* = proc (
ma: MultiAddress, ma: MultiAddress,
err: ref CatchableError): Future[ref TransportListenError] err: ref CatchableError): Future[ref TransportListenError]
@ -55,16 +54,18 @@ const ListenErrorDefault* =
return newTransportListenError(ma, err) return newTransportListenError(ma, err)
# TODO: add {.raises: [TransportError, TransportListenError].} when supported
# by chronos
method start*( method start*(
self: Transport, self: Transport,
addrs: seq[MultiAddress]) addrs: seq[MultiAddress])
{.base, async, raises: [Defect, TransportDefect, TransportListenError].} = {.base, async.} =
## start the transport ## start the transport
## ##
trace "starting transport on addrs", address = $addrs trace "starting transport on addrs", address = $addrs
if addrs.len == 0: if addrs.len == 0:
raise newException(TransportDefect, raise newException(TransportError,
"Transport requires at least one address to be started") "Transport requires at least one address to be started")
self.addrs = addrs self.addrs = addrs

View File

@ -88,9 +88,10 @@ type
proc secure*(self: WsTransport): bool = proc secure*(self: WsTransport): bool =
not (isNil(self.tlsPrivateKey) or isNil(self.tlsCertificate)) not (isNil(self.tlsPrivateKey) or isNil(self.tlsCertificate))
# TODO: add {.raises: [TransportListenError].} when supported by chronos
method start*( method start*(
self: WsTransport, self: WsTransport,
addrs: seq[MultiAddress]) {.async, raises: [Defect, TransportListenError].} = addrs: seq[MultiAddress]) {.async.} =
## listen on the transport ## listen on the transport
## ##

View File

@ -1500,7 +1500,7 @@ suite "Switch":
transport = (upgr: Upgrade) -> Transport => MockTransport.new(upgr) transport = (upgr: Upgrade) -> Transport => MockTransport.new(upgr)
# builder should raise defect with addresses, but no transports # builder should raise defect with addresses, but no transports
expect LPDefect: expect LPError:
discard SwitchBuilder discard SwitchBuilder
.new() .new()
.withRng(rng) # Give the application RNG .withRng(rng) # Give the application RNG
@ -1510,7 +1510,7 @@ suite "Switch":
.build() .build()
# builder should raise defect with transports, but no addresses # builder should raise defect with transports, but no addresses
expect LPDefect: expect LPError:
echo "[testswitch] building switch with no addresses" echo "[testswitch] building switch with no addresses"
discard SwitchBuilder discard SwitchBuilder
.new() .new()
@ -1531,7 +1531,7 @@ suite "Switch":
muxers[MplexCodec] = muxers[MplexCodec] =
MuxerProvider.new((conn: Connection) -> Muxer => Mplex.new(conn), MplexCodec) MuxerProvider.new((conn: Connection) -> Muxer => Mplex.new(conn), MplexCodec)
expect LPDefect: expect LPError:
discard newSwitch( discard newSwitch(
peerInfo = peerInfo, peerInfo = peerInfo,
transports = @[], transports = @[],
@ -1542,7 +1542,7 @@ suite "Switch":
# should raise defect with transports, but no addresses # should raise defect with transports, but no addresses
peerInfo.addrs = @[] peerInfo.addrs = @[]
expect LPDefect: expect LPError:
discard newSwitch( discard newSwitch(
peerInfo = peerInfo, peerInfo = peerInfo,
transports = @[transport(Upgrade.new())], transports = @[transport(Upgrade.new())],

View File

@ -253,7 +253,7 @@ suite "TCP transport":
asyncTest "should raise Defect on start with no addresses": asyncTest "should raise Defect on start with no addresses":
let transport = TcpTransport.new(upgrade = Upgrade()) let transport = TcpTransport.new(upgrade = Upgrade())
expect TransportDefect: expect TransportError:
await transport.start(@[]) await transport.start(@[])
await transport.stop() await transport.stop()

View File

@ -183,7 +183,7 @@ suite "WebSocket transport":
asyncTest "should raise Defect on start with no addresses": asyncTest "should raise Defect on start with no addresses":
let transport = WsTransport.new(upgrade = Upgrade()) let transport = WsTransport.new(upgrade = Upgrade())
expect TransportDefect: expect TransportError:
await transport.start(@[]) await transport.start(@[])
await transport.stop() await transport.stop()