Uncaught exceptions in JS now always propagate with better stack trace.

This commit is contained in:
Yuriy Glukhov 2016-08-27 14:11:41 +03:00
commit 90b0a771e4
2 changed files with 40 additions and 29 deletions

View file

@ -7,11 +7,6 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
when defined(nodejs):
proc alert*(s: cstring) {.importc: "console.log", nodecl.}
else:
proc alert*(s: cstring) {.importc, nodecl.}
proc log*(s: cstring) {.importc: "console.log", varargs, nodecl.} proc log*(s: cstring) {.importc: "console.log", varargs, nodecl.}
type type
@ -101,26 +96,32 @@ proc getStackTrace*(): string = rawWriteStackTrace()
proc unhandledException(e: ref Exception) {. proc unhandledException(e: ref Exception) {.
compilerproc, asmNoStackFrame.} = compilerproc, asmNoStackFrame.} =
when NimStackTrace: var buf = ""
var buf = rawWriteStackTrace() if e.msg != nil and e.msg[0] != '\0':
add(buf, "Error: unhandled exception: ")
add(buf, e.msg)
else: else:
var buf = "" add(buf, "Error: unhandled exception")
if e.msg != nil and e.msg[0] != '\0': add(buf, " [")
add(buf, "Error: unhandled exception: ") add(buf, e.name)
add(buf, e.msg) add(buf, "]\n")
else: when NimStackTrace:
add(buf, "Error: unhandled exception") add(buf, rawWriteStackTrace())
add(buf, " [") let cbuf : cstring = buf
add(buf, e.name) {.emit: """
add(buf, "]\n") if (typeof(Error) !== "undefined") {
alert(buf) throw new Error(`cbuf`);
}
else {
throw `cbuf`;
}
""".}
proc raiseException(e: ref Exception, ename: cstring) {. proc raiseException(e: ref Exception, ename: cstring) {.
compilerproc, asmNoStackFrame.} = compilerproc, asmNoStackFrame.} =
e.name = ename e.name = ename
when not defined(noUnhandledHandler): if excHandler == 0:
if excHandler == 0: unhandledException(e)
unhandledException(e)
when defined(nimphp): when defined(nimphp):
asm """throw new Exception($`e`["message"]);""" asm """throw new Exception($`e`["message"]);"""
else: else:
@ -130,15 +131,15 @@ proc reraiseException() {.compilerproc, asmNoStackFrame.} =
if lastJSError == nil: if lastJSError == nil:
raise newException(ReraiseError, "no exception to reraise") raise newException(ReraiseError, "no exception to reraise")
else: else:
when not defined(noUnhandledHandler): if excHandler == 0:
if excHandler == 0: var isNimException: bool
var isNimException: bool when defined(nimphp):
when defined(nimphp): asm "`isNimException` = isset(`lastJSError`['m_type']);"
asm "`isNimException` = isset(`lastJSError`['m_type']);" else:
else: asm "`isNimException` = lastJSError.m_type;"
asm "`isNimException` = lastJSError.m_type;" if isNimException:
if isNimException: unhandledException(cast[ref Exception](lastJSError))
unhandledException(cast[ref Exception](lastJSError))
asm "throw lastJSError;" asm "throw lastJSError;"
proc raiseOverflow {.exportc: "raiseOverflow", noreturn.} = proc raiseOverflow {.exportc: "raiseOverflow", noreturn.} =
@ -873,3 +874,10 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat, start = 0): int {
# evaluate sign # evaluate sign
number = number * sign number = number * sign
result = i - start result = i - start
when defined(nodejs):
# Deprecated. Use `alert` defined in dom.nim
proc alert*(s: cstring) {.importc: "console.log", nodecl, deprecated.}
else:
# Deprecated. Use `alert` defined in dom.nim
proc alert*(s: cstring) {.importc, nodecl, deprecated.}

View file

@ -57,6 +57,9 @@ that have tuple name:
- Now when you compile console application for Windows, console output - Now when you compile console application for Windows, console output
encoding is automatically set to UTF-8. encoding is automatically set to UTF-8.
- Unhandled exceptions in JavaScript are now thrown regardless ``noUnhandledHandler``
is defined. But now they do their best to provide a readable stack trace.
Library Additions Library Additions
----------------- -----------------