From 1771534030e5b7e8d34b846c5fffde980be21459 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 10 Sep 2024 15:24:58 +0200 Subject: [PATCH] fix(multiaddress): Raise `MaError` instead of `LPError` on `&` (#1145) `LPError` is the top level error type of libp2p, it makes more sense to raise a multi address specific subtype to avoid requiring callers to catch errors too broadly. As `MaError` inherits from `LPError`, existing error handlers will still work fine. --------- Co-authored-by: diegomrsantos --- libp2p/multiaddress.nim | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 69915bd0d..e1668dade 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -12,7 +12,7 @@ {.push raises: [].} {.push public.} -import pkg/chronos, chronicles +import pkg/[chronos, chronicles, results] import std/[nativesockets, net, hashes] import tables, strutils, sets import @@ -25,8 +25,8 @@ import protobuf/minprotobuf, errors, utility -import stew/[base58, base32, endians2, results] -export results, minprotobuf, vbuffer, utility +import stew/[base58, base32, endians2] +export results, minprotobuf, vbuffer, errors, utility logScope: topics = "libp2p multiaddress" @@ -71,6 +71,9 @@ type tcpProtocol udpProtocol +func maErr*(msg: string): ref MaError = + (ref MaError)(msg: msg) + const # These are needed in order to avoid an ambiguity error stemming from # some cint constants with the same name defined in the posix modules @@ -970,23 +973,21 @@ proc append*(m1: var MultiAddress, m2: MultiAddress): MaResult[void] = else: ok() -proc `&`*(m1, m2: MultiAddress): MultiAddress {.raises: [LPError].} = +proc `&`*(m1, m2: MultiAddress): MultiAddress {.raises: [MaError].} = ## Concatenates two addresses ``m1`` and ``m2``, and returns result. ## ## This procedure performs validation of concatenated result and can raise ## exception on error. - ## + concat(m1, m2).valueOr: + raise maErr error - concat(m1, m2).tryGet() - -proc `&=`*(m1: var MultiAddress, m2: MultiAddress) {.raises: [LPError].} = +proc `&=`*(m1: var MultiAddress, m2: MultiAddress) {.raises: [MaError].} = ## Concatenates two addresses ``m1`` and ``m2``. ## ## This procedure performs validation of concatenated result and can raise ## exception on error. - ## - - m1.append(m2).tryGet() + m1.append(m2).isOkOr: + raise maErr error proc `==`*(m1: var MultiAddress, m2: MultiAddress): bool = ## Check of two MultiAddress are equal