summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--changelog.md3
-rw-r--r--lib/system.nim3
-rw-r--r--lib/system/assertions.nim19
-rw-r--r--tests/test_nimscript.nims6
4 files changed, 20 insertions, 11 deletions
diff --git a/changelog.md b/changelog.md
index 3bdf8a511..8b427bfc5 100644
--- a/changelog.md
+++ b/changelog.md
@@ -31,10 +31,11 @@
 - Removed deprecated `iup` module from stdlib, it has already moved to
   [nimble](https://github.com/nim-lang/iup).
 
-
+- `doAssertRaises` now correctly handles foreign exceptions.
 
 ## Language changes
 
+- `nimscript` now handles `except Exception as e`
 - The `cstring` doesn't support `[]=` operator in JS backend.
 
 
diff --git a/lib/system.nim b/lib/system.nim
index e41dfe1ca..fd5eb2dac 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2360,7 +2360,8 @@ when notJSnotNims and hostOS != "standalone":
     ##
     ## **Warning**: Only use this if you know what you are doing.
     currException = exc
-
+elif defined(nimscript):
+  proc getCurrentException*(): ref Exception {.compilerRtl.} = discard
 
 when notJSnotNims:
   {.push stackTrace: off, profiler: off.}
diff --git a/lib/system/assertions.nim b/lib/system/assertions.nim
index c6283c89c..1a4fda123 100644
--- a/lib/system/assertions.nim
+++ b/lib/system/assertions.nim
@@ -81,20 +81,23 @@ template onFailedAssert*(msg, code: untyped): untyped {.dirty.} =
     code
 
 template doAssertRaises*(exception: typedesc, code: untyped) =
-  ## Raises ``AssertionDefect`` if specified ``code`` does not raise the
-  ## specified exception. Example:
+  ## Raises ``AssertionDefect`` if specified ``code`` does not raise `exception`.
+  ## Example:
   ##
   ## .. code-block:: nim
   ##  doAssertRaises(ValueError):
   ##    raise newException(ValueError, "Hello World")
   var wrong = false
+  const begin = "expected raising '" & astToStr(exception) & "', instead"
+  const msgEnd = " by: " & astToStr(code)
+  template raisedForeign = raiseAssert(begin & " raised foreign exception" & msgEnd)
   when Exception is exception:
     try:
       if true:
         code
       wrong = true
-    except Exception:
-      discard
+    except Exception as e: discard
+    except: raisedForeign()
   else:
     try:
       if true:
@@ -102,9 +105,7 @@ template doAssertRaises*(exception: typedesc, code: untyped) =
       wrong = true
     except exception:
       discard
-    except Exception:
-      raiseAssert(astToStr(exception) &
-                  " wasn't raised, another error was raised instead by:\n"&
-                  astToStr(code))
+    except Exception as e: raiseAssert(begin & " raised '" & $e.name & "'" & msgEnd)
+    except: raisedForeign()
   if wrong:
-    raiseAssert(astToStr(exception) & " wasn't raised by:\n" & astToStr(code))
+    raiseAssert(begin & " nothing was raised" & msgEnd)
diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims
index b94146b1e..ea640cac6 100644
--- a/tests/test_nimscript.nims
+++ b/tests/test_nimscript.nims
@@ -81,3 +81,9 @@ block: # #14142
   discard dirExists("/usr")
   discard fileExists("/usr/foo")
   discard findExe("nim")
+
+block:
+  doAssertRaises(AssertionDefect): doAssert false
+  try: doAssert false
+  except Exception as e:
+    discard