From 06f267e125698b8695740e822898ccb29eef5d93 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 21 Jun 2018 16:46:52 -0400 Subject: [PATCH] Prevents errors thrown outside of the run promise from being swallowed Closes #175 --- lib/zone.js | 6 ++++-- test/test.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/zone.js b/lib/zone.js index f4b8084..1a4e903 100644 --- a/lib/zone.js +++ b/lib/zone.js @@ -41,9 +41,11 @@ Task.prototype.run = function(ctx, args){ zone.execHookR("afterTask", this); } catch(err) { Zone.current = previousZone; - if(!this.nestedTask) + if(!this.nestedTask) { zone.execHookR("afterTask", this); - if(this.catchErrors !== false) { + } + + if(this.catchErrors !== false && !this.zone.isResolved) { zone.errors.push(err); } else { throw err; diff --git a/test/test.js b/test/test.js index 0e59af7..8972051 100644 --- a/test/test.js +++ b/test/test.js @@ -905,6 +905,27 @@ if(isBrowser && !isWorker) { }) .then(done, done); }); + + it("Event handlers ran after the Zone completes throw their error", function(done){ + var el = document.createElement("div"); + + new Zone().run(function(){ + el.addEventListener("some-event", function(){ + throw new Error("Hello world!"); + }); + }) + .then(function(){ + var errorNotSwallowed = false; + window.onerror = function(){ + window.onerror = null; + errorNotSwallowed = true; + return true; + }; + el.dispatchEvent(new Event("some-event")); + assert.ok(errorNotSwallowed, "Threw an error, not swallowed"); + }) + .then(done, done); + }); }); describe("when third-party library (i.e. affirm) also wraps events (i.e. addEventListener) in strict-mode", function(){