mirror of
https://github.com/vacp2p/nim-libp2p.git
synced 2025-03-01 08:30:29 +00:00
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:
parent
d4a65503ac
commit
9a79de0c62
@ -136,7 +136,7 @@ proc withNameResolver*(b: SwitchBuilder, nameResolver: NameResolver): SwitchBuil
|
||||
b
|
||||
|
||||
proc build*(b: SwitchBuilder): Switch
|
||||
{.raises: [Defect, LPDefect, LPError].} =
|
||||
{.raises: [Defect, LPError].} =
|
||||
|
||||
if b.rng == nil: # newRng could fail
|
||||
raise newException(Defect, "Cannot initialize RNG")
|
||||
@ -210,7 +210,7 @@ proc newStandardSwitch*(
|
||||
maxOut = -1,
|
||||
maxConnsPerPeer = MaxConnectionsPerPeer,
|
||||
nameResolver: NameResolver = nil): Switch
|
||||
{.raises: [Defect, LPDefect, LPError].} =
|
||||
{.raises: [Defect, LPError].} =
|
||||
if SecureProtocol.Secio in secureManagers:
|
||||
quit("Secio is deprecated!") # use of secio is unsafe
|
||||
|
||||
|
@ -9,8 +9,6 @@ type
|
||||
# Base exception type for libp2p
|
||||
LPError* = object of CatchableError
|
||||
|
||||
LPDefect* = object of CatchableError
|
||||
|
||||
func toException*(e: cstring): ref LPError =
|
||||
(ref LPError)(msg: $e)
|
||||
|
||||
|
@ -235,7 +235,8 @@ proc stop*(s: Switch) {.async.} =
|
||||
|
||||
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
|
||||
for t in s.transports:
|
||||
let addrs = s.peerInfo.addrs.filterIt(
|
||||
@ -261,13 +262,13 @@ proc newSwitch*(peerInfo: PeerInfo,
|
||||
connManager: ConnManager,
|
||||
ms: MultistreamSelect,
|
||||
nameResolver: NameResolver = nil): Switch
|
||||
{.raises: [Defect, LPDefect, LPError].} =
|
||||
{.raises: [Defect, LPError].} =
|
||||
|
||||
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:
|
||||
raise newException(LPDefect, "Provide at least one address")
|
||||
raise newException(LPError, "Provide at least one address")
|
||||
|
||||
if secureManagers.len == 0:
|
||||
raise newException(LPError, "Provide at least one secure manager")
|
||||
|
@ -129,9 +129,10 @@ proc new*(
|
||||
inc getTcpTransportTracker().opened
|
||||
return transport
|
||||
|
||||
# TODO: add {.raises: [TransportListenError].} when supported by chronos
|
||||
method start*(
|
||||
self: TcpTransport,
|
||||
addrs: seq[MultiAddress]) {.async, raises: [Defect, TransportListenError].} =
|
||||
addrs: seq[MultiAddress]) {.async.} =
|
||||
## listen on the transport
|
||||
##
|
||||
|
||||
|
@ -21,7 +21,6 @@ logScope:
|
||||
topics = "libp2p transport"
|
||||
|
||||
type
|
||||
TransportDefect* = object of Defect
|
||||
ListenErrorCallback* = proc (
|
||||
ma: MultiAddress,
|
||||
err: ref CatchableError): Future[ref TransportListenError]
|
||||
@ -55,16 +54,18 @@ const ListenErrorDefault* =
|
||||
|
||||
return newTransportListenError(ma, err)
|
||||
|
||||
# TODO: add {.raises: [TransportError, TransportListenError].} when supported
|
||||
# by chronos
|
||||
method start*(
|
||||
self: Transport,
|
||||
addrs: seq[MultiAddress])
|
||||
{.base, async, raises: [Defect, TransportDefect, TransportListenError].} =
|
||||
{.base, async.} =
|
||||
## start the transport
|
||||
##
|
||||
|
||||
trace "starting transport on addrs", address = $addrs
|
||||
if addrs.len == 0:
|
||||
raise newException(TransportDefect,
|
||||
raise newException(TransportError,
|
||||
"Transport requires at least one address to be started")
|
||||
|
||||
self.addrs = addrs
|
||||
|
@ -88,9 +88,10 @@ type
|
||||
proc secure*(self: WsTransport): bool =
|
||||
not (isNil(self.tlsPrivateKey) or isNil(self.tlsCertificate))
|
||||
|
||||
# TODO: add {.raises: [TransportListenError].} when supported by chronos
|
||||
method start*(
|
||||
self: WsTransport,
|
||||
addrs: seq[MultiAddress]) {.async, raises: [Defect, TransportListenError].} =
|
||||
addrs: seq[MultiAddress]) {.async.} =
|
||||
## listen on the transport
|
||||
##
|
||||
|
||||
|
@ -1500,7 +1500,7 @@ suite "Switch":
|
||||
transport = (upgr: Upgrade) -> Transport => MockTransport.new(upgr)
|
||||
|
||||
# builder should raise defect with addresses, but no transports
|
||||
expect LPDefect:
|
||||
expect LPError:
|
||||
discard SwitchBuilder
|
||||
.new()
|
||||
.withRng(rng) # Give the application RNG
|
||||
@ -1510,7 +1510,7 @@ suite "Switch":
|
||||
.build()
|
||||
|
||||
# builder should raise defect with transports, but no addresses
|
||||
expect LPDefect:
|
||||
expect LPError:
|
||||
echo "[testswitch] building switch with no addresses"
|
||||
discard SwitchBuilder
|
||||
.new()
|
||||
@ -1531,7 +1531,7 @@ suite "Switch":
|
||||
muxers[MplexCodec] =
|
||||
MuxerProvider.new((conn: Connection) -> Muxer => Mplex.new(conn), MplexCodec)
|
||||
|
||||
expect LPDefect:
|
||||
expect LPError:
|
||||
discard newSwitch(
|
||||
peerInfo = peerInfo,
|
||||
transports = @[],
|
||||
@ -1542,7 +1542,7 @@ suite "Switch":
|
||||
|
||||
# should raise defect with transports, but no addresses
|
||||
peerInfo.addrs = @[]
|
||||
expect LPDefect:
|
||||
expect LPError:
|
||||
discard newSwitch(
|
||||
peerInfo = peerInfo,
|
||||
transports = @[transport(Upgrade.new())],
|
||||
|
@ -253,7 +253,7 @@ suite "TCP transport":
|
||||
asyncTest "should raise Defect on start with no addresses":
|
||||
let transport = TcpTransport.new(upgrade = Upgrade())
|
||||
|
||||
expect TransportDefect:
|
||||
expect TransportError:
|
||||
await transport.start(@[])
|
||||
|
||||
await transport.stop()
|
||||
|
@ -183,7 +183,7 @@ suite "WebSocket transport":
|
||||
asyncTest "should raise Defect on start with no addresses":
|
||||
let transport = WsTransport.new(upgrade = Upgrade())
|
||||
|
||||
expect TransportDefect:
|
||||
expect TransportError:
|
||||
await transport.start(@[])
|
||||
|
||||
await transport.stop()
|
||||
|
Loading…
x
Reference in New Issue
Block a user