Merge pull request #4670 from yglukhov/js-getcurex

Fixed getCurrentException and getCurrentExceptionMsg. Closes #4635
This commit is contained in:
Andreas Rumpf
2016-08-29 16:25:11 +02:00
committed by GitHub
2 changed files with 26 additions and 12 deletions

View File

@@ -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;"

View File

@@ -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: