summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-08-29 16:25:11 +0200
committerGitHub <noreply@github.com>2016-08-29 16:25:11 +0200
commit283f8e9c8cb1ca1efc2d8c7c8a85a9cc86f579d0 (patch)
tree8214d6f3ca0b03cadbb44cb1ee6fce58b2876cab
parente92a7fd50841e91e538bd7c1e5c6efa85b54174c (diff)
parentf12f27c185f40576633473ee548636c72f146a90 (diff)
downloadNim-283f8e9c8cb1ca1efc2d8c7c8a85a9cc86f579d0.tar.gz
Merge pull request #4670 from yglukhov/js-getcurex
Fixed getCurrentException and getCurrentExceptionMsg. Closes #4635
-rw-r--r--lib/system/jssys.nim32
-rw-r--r--tests/exception/texceptions.nim6
2 files changed, 26 insertions, 12 deletions
diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim
index abec44bbb..31d283cd7 100644
--- a/lib/system/jssys.nim
+++ b/lib/system/jssys.nim
@@ -47,11 +47,30 @@ proc nimCharToStr(x: char): string {.compilerproc.} =
   result = newString(1)
   result[0] = x
 
+proc isNimException(): bool {.asmNoStackFrame.} =
+  when defined(nimphp):
+    asm "return isset(`lastJSError`['m_type']);"
+  else:
+    asm "return `lastJSError`.m_type;"
+
+proc getCurrentException*(): ref Exception =
+  if isNimException(): result = cast[ref Exception](lastJSError)
+
 proc getCurrentExceptionMsg*(): string =
   if lastJSError != nil:
-    return $lastJSError.message
-  else:
-    return ""
+    if isNimException():
+      return cast[Exception](lastJSError).msg
+    else:
+      when not defined(nimphp):
+        var msg: cstring
+        {.emit: """
+        if (`lastJSError`.message !== undefined) {
+          `msg` = `lastJSError`.message;
+        }
+        """.}
+        if not msg.isNil:
+          return $msg
+  return ""
 
 proc auxWriteStackTrace(f: PCallFrame): string =
   type
@@ -130,12 +149,7 @@ proc reraiseException() {.compilerproc, asmNoStackFrame.} =
     raise newException(ReraiseError, "no exception to reraise")
   else:
     if excHandler == 0:
-      var isNimException: bool
-      when defined(nimphp):
-        asm "`isNimException` = isset(`lastJSError`['m_type']);"
-      else:
-        asm "`isNimException` = lastJSError.m_type;"
-      if isNimException:
+      if isNimException():
         unhandledException(cast[ref Exception](lastJSError))
 
     asm "throw lastJSError;"
diff --git a/tests/exception/texceptions.nim b/tests/exception/texceptions.nim
index bdf338599..b30b3874b 100644
--- a/tests/exception/texceptions.nim
+++ b/tests/exception/texceptions.nim
@@ -9,7 +9,7 @@ FINALLY
 RECOVER
 
 BEFORE
-EXCEPT
+EXCEPT: IOError: hi
 FINALLY
 '''
 """
@@ -52,10 +52,10 @@ echo ""
 proc return_in_except =
   try:
     echo "BEFORE"
-    raise newException(IOError, "")
+    raise newException(IOError, "hi")
 
   except:
-    echo "EXCEPT"
+    echo "EXCEPT: ", getCurrentException().name, ": ", getCurrentExceptionMsg()
     return
 
   finally: