Change UnpackError with UnpackDefect (#14457)
* Change `UnpackError` with `UnpackDefect` The deprecation warning for `UnpackError` exception raised by some `inline` procedures in the Nim standard library propagates to the user code. If the user code has a requirement for building without warnings this is a problem for the successful execution of the tests. In order to resolve this, all occurrences of `UnpackError` in the Nim code base are changed to `UnpackDefect`. Only the type alias is retained to not break other people's user code since `UnpackError` is exported type. * Remove the catching of `UnpackDefect` Defect exceptions should not be cached, because they indicate problem in the API usage. The code in `nimblesocket.nim` is rewritten to first check whether there is a value set into the `knownDomain` variable from the `Option` type before usage.
This commit is contained in:
parent
0533c43547
commit
cc65ae6011
2 changed files with 10 additions and 9 deletions
|
|
@ -445,9 +445,10 @@ proc getSockDomain*(socket: SocketHandle): Domain =
|
||||||
if getsockname(socket, cast[ptr SockAddr](addr(name)),
|
if getsockname(socket, cast[ptr SockAddr](addr(name)),
|
||||||
addr(namelen)) == -1'i32:
|
addr(namelen)) == -1'i32:
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError())
|
||||||
try:
|
let knownDomain = toKnownDomain(name.sin6_family.cint)
|
||||||
result = toKnownDomain(name.sin6_family.cint).get()
|
if knownDomain.isSome:
|
||||||
except UnpackError:
|
result = knownDomain.get()
|
||||||
|
else:
|
||||||
raise newException(IOError, "Unknown socket family in getSockDomain")
|
raise newException(IOError, "Unknown socket family in getSockDomain")
|
||||||
|
|
||||||
proc getAddrString*(sockAddr: ptr SockAddr): string =
|
proc getAddrString*(sockAddr: ptr SockAddr): string =
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@
|
||||||
## assert found.isSome and found.get() == 2
|
## assert found.isSome and found.get() == 2
|
||||||
##
|
##
|
||||||
## The `get` operation demonstrated above returns the underlying value, or
|
## The `get` operation demonstrated above returns the underlying value, or
|
||||||
## raises `UnpackError` if there is no value. Note that `UnpackError`
|
## raises `UnpackDefect` if there is no value. Note that `UnpackDefect`
|
||||||
## inherits from `system.Defect`, and should therefore never be caught.
|
## inherits from `system.Defect`, and should therefore never be caught.
|
||||||
## Instead, rely on checking if the option contains a value with
|
## Instead, rely on checking if the option contains a value with
|
||||||
## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
|
## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
|
||||||
|
|
@ -178,11 +178,11 @@ proc get*[T](self: Option[T]): lent T {.inline.} =
|
||||||
a = some(42)
|
a = some(42)
|
||||||
b = none(string)
|
b = none(string)
|
||||||
assert a.get == 42
|
assert a.get == 42
|
||||||
doAssertRaises(UnpackError):
|
doAssertRaises(UnpackDefect):
|
||||||
echo b.get
|
echo b.get
|
||||||
|
|
||||||
if self.isNone:
|
if self.isNone:
|
||||||
raise newException(UnpackError, "Can't obtain a value from a `none`")
|
raise newException(UnpackDefect, "Can't obtain a value from a `none`")
|
||||||
result = self.val
|
result = self.val
|
||||||
|
|
||||||
proc get*[T](self: Option[T], otherwise: T): T {.inline.} =
|
proc get*[T](self: Option[T], otherwise: T): T {.inline.} =
|
||||||
|
|
@ -208,11 +208,11 @@ proc get*[T](self: var Option[T]): var T {.inline.} =
|
||||||
a = some(42)
|
a = some(42)
|
||||||
b = none(string)
|
b = none(string)
|
||||||
assert a.get == 42
|
assert a.get == 42
|
||||||
doAssertRaises(UnpackError):
|
doAssertRaises(UnpackDefect):
|
||||||
echo b.get
|
echo b.get
|
||||||
|
|
||||||
if self.isNone:
|
if self.isNone:
|
||||||
raise newException(UnpackError, "Can't obtain a value from a `none`")
|
raise newException(UnpackDefect, "Can't obtain a value from a `none`")
|
||||||
return self.val
|
return self.val
|
||||||
|
|
||||||
proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} =
|
proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} =
|
||||||
|
|
@ -411,7 +411,7 @@ when isMainModule:
|
||||||
check some("a").isSome
|
check some("a").isSome
|
||||||
|
|
||||||
test "none":
|
test "none":
|
||||||
expect UnpackError:
|
expect UnpackDefect:
|
||||||
discard none(int).get()
|
discard none(int).get()
|
||||||
check(none(int).isNone)
|
check(none(int).isNone)
|
||||||
check(not none(string).isSome)
|
check(not none(string).isSome)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue