better setupErrorHandler
This commit is contained in:
parent
8666d8d6d6
commit
2d1e656061
1 changed files with 6 additions and 12 deletions
|
|
@ -284,23 +284,17 @@ proc setOnHashChange*(action: proc (hashPart: cstring)) =
|
||||||
|
|
||||||
{.push stackTrace:off.}
|
{.push stackTrace:off.}
|
||||||
|
|
||||||
proc setupErrorHandler*(useAlert=false) =
|
proc setupErrorHandler*() =
|
||||||
## Installs an error handler that transforms native JS unhandled
|
## Installs an error handler that transforms native JS unhandled
|
||||||
## exceptions into Nim based stack traces. If `useAlert` is false,
|
## exceptions into Nim based stack traces. If `useAlert` is false,
|
||||||
## the error message is put into the console, otherwise `alert`
|
## the error message is put into the console, otherwise `alert`
|
||||||
## is called.
|
## is called.
|
||||||
proc stackTraceAsCstring(): cstring = cstring(getStackTrace())
|
proc stackTraceAsCstring(): cstring = cstring(getStackTrace())
|
||||||
{.emit: """
|
var onerror {.importc: "window.onerror".} =
|
||||||
window.onerror = function(msg, url, line, col, error) {
|
proc (msg, url: cstring, line, col: int, error: cstring): bool =
|
||||||
var x = "Error: " + msg + "\n" + `stackTraceAsCstring`()
|
var x = cstring"Error: " & msg & "\n" & stackTraceAsCstring()
|
||||||
if (`useAlert`)
|
kout(x)
|
||||||
alert(x);
|
return true # suppressErrorAlert
|
||||||
else
|
|
||||||
console.log(x);
|
|
||||||
var suppressErrorAlert = true;
|
|
||||||
return suppressErrorAlert;
|
|
||||||
};""".}
|
|
||||||
|
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
proc prepend(parent, kid: Element) =
|
proc prepend(parent, kid: Element) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue