mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-02 13:53:11 +00:00
Fix: nested without calls in generic code
This commit is contained in:
parent
4abeef5c36
commit
90ea780ba9
@ -1,15 +1,15 @@
|
||||
import std/options
|
||||
|
||||
var captureEnabled {.global, compileTime.}: bool
|
||||
var captures {.global, compileTime.}: int
|
||||
var errorVariable: ptr ref CatchableError
|
||||
|
||||
template captureBindError*(error: var ref CatchableError, expression): auto =
|
||||
let previousErrorVariable = errorVariable
|
||||
errorVariable = addr error
|
||||
|
||||
static: captureEnabled = true
|
||||
static: inc captures
|
||||
let evaluated = expression
|
||||
static: captureEnabled = false
|
||||
static: dec captures
|
||||
|
||||
errorVariable = previousErrorVariable
|
||||
|
||||
@ -19,6 +19,6 @@ func error[T](option: Option[T]): ref CatchableError =
|
||||
newException(ValueError, "Option is set to `none`")
|
||||
|
||||
template bindFailed*(expression) =
|
||||
when captureEnabled:
|
||||
when captures > 0:
|
||||
mixin error
|
||||
errorVariable[] = expression.error
|
||||
|
||||
@ -294,6 +294,22 @@ suite "result":
|
||||
|
||||
foo()
|
||||
|
||||
test "without statement with error works in nested generic calls":
|
||||
proc works(_: type int): ?!int =
|
||||
without _ =? int.failure "error1", err:
|
||||
return success 42
|
||||
|
||||
proc fails(_: type int): ?!int =
|
||||
return failure "error2"
|
||||
|
||||
proc foo =
|
||||
without a =? int.works() and b =? int.fails(), error:
|
||||
check error.msg == "error2"
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user