From 9110d7dece95c439f9fbc9c8fdf234f4f97236f8 Mon Sep 17 00:00:00 2001 From: Gene Pavlovsky Date: Fri, 2 Feb 2024 13:11:38 +0100 Subject: [PATCH 1/2] Add exception handling to `CallbackList.invoke` to prevent this kind of shit: https://try.haxe.org/#50b2a5Ce --- src/tink/core/Callback.hx | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/tink/core/Callback.hx b/src/tink/core/Callback.hx index 23453da..03c0e72 100644 --- a/src/tink/core/Callback.hx +++ b/src/tink/core/Callback.hx @@ -226,9 +226,12 @@ class CallbackList extends SimpleDisposable { if (destructive) dispose(); + // Invoke all the cells, even if some of them throw exceptions. Another option would be to fail fast, + // but I think it's not nice to let one rogue cell mess things up for all the others. + final errors = []; var length = cells.length; for (i in 0...length) - cells[i].invoke(data); + try cells[i].invoke(data) catch (err) errors.push(err); busy = false; @@ -241,6 +244,22 @@ class CallbackList extends SimpleDisposable { if (queue.length > 0) queue.shift()(); } + + #if debug_tink + #if js + function dump(str) js.Browser.console.error(str); + #elseif sys + function dump(str) Sys.stderr().writeString('${str}\n'); + #else + function dump(str) trace(str); + #end + for (i => err in errors) dump('Callback $i barfed: $err'); + #end + switch errors.length { + case 0: + case 1: throw errors[0]; + case _: throw errors; + } } }); From 64e42c58a0fba4a00c86d15a32815791318fd103 Mon Sep 17 00:00:00 2001 From: Gene Pavlovsky Date: Fri, 2 Feb 2024 13:23:58 +0100 Subject: [PATCH 2/2] Add missing type definition --- src/tink/core/Callback.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tink/core/Callback.hx b/src/tink/core/Callback.hx index 03c0e72..bff08f5 100644 --- a/src/tink/core/Callback.hx +++ b/src/tink/core/Callback.hx @@ -231,7 +231,7 @@ class CallbackList extends SimpleDisposable { final errors = []; var length = cells.length; for (i in 0...length) - try cells[i].invoke(data) catch (err) errors.push(err); + try cells[i].invoke(data) catch (err:Dynamic) errors.push(err); busy = false;