add system.onUnhandledException feature

This commit is contained in:
Andreas Rumpf 2017-07-12 15:37:41 +02:00
commit 57ed077c1e

View file

@ -230,6 +230,18 @@ when false:
pushCurrentException(e) pushCurrentException(e)
c_longjmp(excHandler.context, 1) c_longjmp(excHandler.context, 1)
var onUnhandledException*: (proc (errorMsg: string) {.
nimcall.}) ## set this error \
## handler to override the existing behaviour on an unhandled exception.
## The default is to write a stacktrace to ``stderr`` and then call ``quit(1)``.
## Unstable API.
template unhandled(buf, body) =
if onUnhandledException != nil:
onUnhandledException(buf)
else:
body
proc raiseExceptionAux(e: ref Exception) = proc raiseExceptionAux(e: ref Exception) =
if localRaiseHook != nil: if localRaiseHook != nil:
if not localRaiseHook(e): return if not localRaiseHook(e): return
@ -260,7 +272,9 @@ proc raiseExceptionAux(e: ref Exception) =
add(buf, " [") add(buf, " [")
add(buf, $e.name) add(buf, $e.name)
add(buf, "]\n") add(buf, "]\n")
unhandled(buf):
showErrorMessage(buf) showErrorMessage(buf)
quitOrDebug()
else: else:
# ugly, but avoids heap allocations :-) # ugly, but avoids heap allocations :-)
template xadd(buf, s, slen: expr) = template xadd(buf, s, slen: expr) =
@ -276,6 +290,7 @@ proc raiseExceptionAux(e: ref Exception) =
add(buf, " [") add(buf, " [")
xadd(buf, e.name, e.name.len) xadd(buf, e.name, e.name.len)
add(buf, "]\n") add(buf, "]\n")
unhandled(buf):
showErrorMessage(buf) showErrorMessage(buf)
quitOrDebug() quitOrDebug()