Fix: without statement with error works in nested calls

This commit is contained in:
Mark Spanbroek 2022-08-08 18:00:10 +02:00
parent 13c7ff7671
commit f6363e19d3
2 changed files with 19 additions and 1 deletions

View File

@ -4,10 +4,15 @@ var captureEnabled {.global, compileTime.}: bool
var errorVariable: ptr ref CatchableError
template captureBindError*(error: var ref CatchableError, expression): auto =
static: captureEnabled = true
let previousErrorVariable = errorVariable
errorVariable = addr error
static: captureEnabled = true
let evaluated = expression
static: captureEnabled = false
errorVariable = previousErrorVariable
evaluated
func error[T](option: Option[T]): ref CatchableError =

View File

@ -281,6 +281,19 @@ suite "result":
check e2.msg == "error2"
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":
check parseInt("42").catch == 42.success
check parseInt("foo").catch.error of ValueError