Skip to content

Commit 7e489a8

Browse files
Take into account exceptions thrown outside of Haxe-generated js (fixes #14)
1 parent cbaa3ae commit 7e489a8

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

display.hxml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-cp src
2+
-cp test
3+
-debug
4+
-lib sourcemap
5+
-D JSTACK_FORMAT=vscode
6+
7+
extraParams.hxml
8+
9+
-main Test
10+
-js build/test.js

format/js/haxe/CallStack.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CallStack {
2424
for (site in callsites) {
2525
if (wrapCallSite != null) site = wrapCallSite(site);
2626
var method = null;
27-
var fullName :String = site.getFunctionName();
27+
var fullName:String = site.getFunctionName();
2828
if (fullName != null) {
2929
var idx = fullName.lastIndexOf(".");
3030
if (idx >= 0) {

src/jstack/js/JStack.hx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ class JStack {
4040
/**
4141
* Overwrite this method to handle uncaught exceptions manually.
4242
* Any returned value will be written to stderr.
43-
* @param e - Uncaught exception.
43+
* @param e - Uncaught exception. It's `js.Error` most of the time,
44+
* but it also can be any other type if thrown outside of Haxe-generated code.
4445
*/
4546
@:access(haxe.CallStack)
46-
static public dynamic function uncaughtExceptionHandler (e:Error) : String {
47-
var stack = CallStack.getStack(e).map(improveStackItem);
48-
var error = e.message + CallStack.toString(stack) + '\n';
49-
return error;
47+
static public dynamic function uncaughtExceptionHandler (e:Dynamic) : String {
48+
if(untyped __js__("{0} instanceof Error", e)) {
49+
var stack = CallStack.getStack(e).map(improveStackItem);
50+
var error = e.message + CallStack.toString(stack) + '\n';
51+
return error;
52+
} else {
53+
return 'Uncaught exception: ' + Std.string(e);
54+
}
5055
}
5156

5257
function new () {
@@ -148,7 +153,7 @@ class JStack {
148153
* @param e -
149154
*/
150155
static function _uncaughtExceptionHandler (e:Dynamic) {
151-
var error = uncaughtExceptionHandler(@:privateAccess CallStack.lastException);
156+
var error = uncaughtExceptionHandler(e);
152157
if(error != null && error.length > 0) {
153158
untyped __js__('console.log({0})', error);
154159
if (isNode()) {

0 commit comments

Comments
 (0)