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 <diego@status.im>
This commit is contained in:
Etan Kissling 2024-09-10 15:24:58 +02:00 committed by GitHub
parent 21a444197c
commit 1771534030
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 11 deletions

View File

@ -12,7 +12,7 @@
{.push raises: [].} {.push raises: [].}
{.push public.} {.push public.}
import pkg/chronos, chronicles import pkg/[chronos, chronicles, results]
import std/[nativesockets, net, hashes] import std/[nativesockets, net, hashes]
import tables, strutils, sets import tables, strutils, sets
import import
@ -25,8 +25,8 @@ import
protobuf/minprotobuf, protobuf/minprotobuf,
errors, errors,
utility utility
import stew/[base58, base32, endians2, results] import stew/[base58, base32, endians2]
export results, minprotobuf, vbuffer, utility export results, minprotobuf, vbuffer, errors, utility
logScope: logScope:
topics = "libp2p multiaddress" topics = "libp2p multiaddress"
@ -71,6 +71,9 @@ type
tcpProtocol tcpProtocol
udpProtocol udpProtocol
func maErr*(msg: string): ref MaError =
(ref MaError)(msg: msg)
const const
# These are needed in order to avoid an ambiguity error stemming from # These are needed in order to avoid an ambiguity error stemming from
# some cint constants with the same name defined in the posix modules # 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: else:
ok() ok()
proc `&`*(m1, m2: MultiAddress): MultiAddress {.raises: [LPError].} = proc `&`*(m1, m2: MultiAddress): MultiAddress {.raises: [MaError].} =
## Concatenates two addresses ``m1`` and ``m2``, and returns result. ## Concatenates two addresses ``m1`` and ``m2``, and returns result.
## ##
## This procedure performs validation of concatenated result and can raise ## This procedure performs validation of concatenated result and can raise
## exception on error. ## exception on error.
## concat(m1, m2).valueOr:
raise maErr error
concat(m1, m2).tryGet() proc `&=`*(m1: var MultiAddress, m2: MultiAddress) {.raises: [MaError].} =
proc `&=`*(m1: var MultiAddress, m2: MultiAddress) {.raises: [LPError].} =
## Concatenates two addresses ``m1`` and ``m2``. ## Concatenates two addresses ``m1`` and ``m2``.
## ##
## This procedure performs validation of concatenated result and can raise ## This procedure performs validation of concatenated result and can raise
## exception on error. ## exception on error.
## m1.append(m2).isOkOr:
raise maErr error
m1.append(m2).tryGet()
proc `==`*(m1: var MultiAddress, m2: MultiAddress): bool = proc `==`*(m1: var MultiAddress, m2: MultiAddress): bool =
## Check of two MultiAddress are equal ## Check of two MultiAddress are equal