diff --git a/questionable/private/binderror.nim b/questionable/private/binderror.nim index 372f256..9af77ad 100644 --- a/questionable/private/binderror.nim +++ b/questionable/private/binderror.nim @@ -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 = diff --git a/testmodules/result/test.nim b/testmodules/result/test.nim index d39925d..94ed4c7 100644 --- a/testmodules/result/test.nim +++ b/testmodules/result/test.nim @@ -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