mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-06 15:53:07 +00:00
Fix: without statement with error works in nested calls
This commit is contained in:
parent
13c7ff7671
commit
f6363e19d3
@ -4,10 +4,15 @@ var captureEnabled {.global, compileTime.}: bool
|
|||||||
var errorVariable: ptr ref CatchableError
|
var errorVariable: ptr ref CatchableError
|
||||||
|
|
||||||
template captureBindError*(error: var ref CatchableError, expression): auto =
|
template captureBindError*(error: var ref CatchableError, expression): auto =
|
||||||
static: captureEnabled = true
|
let previousErrorVariable = errorVariable
|
||||||
errorVariable = addr error
|
errorVariable = addr error
|
||||||
|
|
||||||
|
static: captureEnabled = true
|
||||||
let evaluated = expression
|
let evaluated = expression
|
||||||
static: captureEnabled = false
|
static: captureEnabled = false
|
||||||
|
|
||||||
|
errorVariable = previousErrorVariable
|
||||||
|
|
||||||
evaluated
|
evaluated
|
||||||
|
|
||||||
func error[T](option: Option[T]): ref CatchableError =
|
func error[T](option: Option[T]): ref CatchableError =
|
||||||
|
|||||||
@ -281,6 +281,19 @@ suite "result":
|
|||||||
check e2.msg == "error2"
|
check e2.msg == "error2"
|
||||||
check e1.msg == "error1"
|
check e1.msg == "error1"
|
||||||
|
|
||||||
|
test "without statements with error work in nested calls":
|
||||||
|
proc bar(): ?!int =
|
||||||
|
without _ =? int.failure "error", err:
|
||||||
|
return failure err.msg
|
||||||
|
|
||||||
|
proc foo() =
|
||||||
|
without _ =? bar(), err:
|
||||||
|
check err.msg == "error"
|
||||||
|
return
|
||||||
|
fail()
|
||||||
|
|
||||||
|
foo()
|
||||||
|
|
||||||
test "catch can be used to convert exceptions to results":
|
test "catch can be used to convert exceptions to results":
|
||||||
check parseInt("42").catch == 42.success
|
check parseInt("42").catch == 42.success
|
||||||
check parseInt("foo").catch.error of ValueError
|
check parseInt("foo").catch.error of ValueError
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user