Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix #1888, catch all exceptions in interactive-worker with watchFunct…
Browse files Browse the repository at this point in the history
…ion/watchPromise
  • Loading branch information
ianb committed Nov 10, 2016
1 parent 92d0051 commit f693b9f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
15 changes: 10 additions & 5 deletions addon/data/error-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* globals FILENAME, exports */
/* exported unhandled, makeError, watchFunction */
/* exported unhandled, makeError, watchFunction, watchPromise */
/** Similar to lib/errors.js, some helpers to catch or handle errors */

/** Call with an error object (with .name, .message, .help, etc) */
Expand Down Expand Up @@ -39,18 +39,23 @@ function watchFunction(func) {
try {
result = func.apply(this, arguments);
} catch (e) {
console.error("------Error in worker script", getFilename(), ":", String(e));
console.error("------Error in worker script:", e+"");
console.error(e.stack);
console.error("Called from:");
console.trace();
console.error("------------------------------------------------------------");
unhandled(makeError(e));
throw e;
}
return result;
};
}

function watchPromise(promise) {
return promise.catch((e) => {
console.error("------Error in promise:", e+"");
console.error(e.stack);
unhandled(makeError(e));
});
}

if (typeof exports !== "undefined") {
exports.unhandled = unhandled;
exports.getFilename = getFilename;
Expand Down
25 changes: 14 additions & 11 deletions addon/data/selector-ui.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals util, window, document, console */
/* globals util, window, document, console, watchFunction */
/* exported ui */

const ui = (function () { // eslint-disable-line no-unused-vars
Expand Down Expand Up @@ -132,7 +132,7 @@ const ui = (function () { // eslint-disable-line no-unused-vars

initSizeWatch: function () {
this.stopSizeWatch();
this.sizeTracking.timer = setInterval(this.updateElementSize.bind(this), 2000);
this.sizeTracking.timer = setInterval(watchFunction(this.updateElementSize.bind(this)), 2000);
window.addEventListener("resize", this.onResize, true);
},

Expand All @@ -153,9 +153,9 @@ const ui = (function () { // eslint-disable-line no-unused-vars
if (this.sizeTracking.windowDelayer) {
clearTimeout(this.sizeTracking.windowDelayer);
}
this.sizeTracking.windowDelayer = setTimeout(() => {
this.sizeTracking.windowDelayer = setTimeout(watchFunction(() => {
this.updateElementSize(true);
}, 100);
}), 100);
},

getElementFromPoint: function (x, y) {
Expand All @@ -176,7 +176,7 @@ const ui = (function () { // eslint-disable-line no-unused-vars
}
};

iframe.onResize = iframe.onResize.bind(iframe);
iframe.onResize = watchFunction(iframe.onResize.bind(iframe));

/** Represents the shadow overlay that covers the whole page */
let WholePageOverlay = exports.WholePageOverlay = {
Expand Down Expand Up @@ -205,9 +205,12 @@ const ui = (function () { // eslint-disable-line no-unused-vars
Save full page
</div>
`;
this.el.querySelector(".pageshot-myshots").addEventListener("click", callbacks.onOpenMyShots, false);
this.el.querySelector(".pageshot-visible").addEventListener("click", callbacks.onClickVisible, false);
this.el.querySelector(".pageshot-full-page").addEventListener("click", callbacks.onClickFullPage, false);
this.el.querySelector(".pageshot-myshots").addEventListener(
"click", watchFunction(callbacks.onOpenMyShots), false);
this.el.querySelector(".pageshot-visible").addEventListener(
"click", watchFunction(callbacks.onClickVisible), false);
this.el.querySelector(".pageshot-full-page").addEventListener(
"click", watchFunction(callbacks.onClickFullPage), false);
this.movingEl = this.el.querySelector(".pageshot-moving-element");
iframe.document.body.appendChild(this.el);
this.resetPosition();
Expand Down Expand Up @@ -245,7 +248,7 @@ const ui = (function () { // eslint-disable-line no-unused-vars

};

WholePageOverlay.onScroll = WholePageOverlay.onScroll.bind(WholePageOverlay);
WholePageOverlay.onScroll = watchFunction(WholePageOverlay.onScroll.bind(WholePageOverlay));

let movements = ["topLeft", "top", "topRight", "left", "right", "bottomLeft", "bottom", "bottomRight"];

Expand Down Expand Up @@ -448,7 +451,7 @@ const ui = (function () { // eslint-disable-line no-unused-vars
".pageshot-cancel": "onCancel"
};
Object.keys(methods).forEach((selector) => {
this.el.querySelector(selector).addEventListener("click", (event) => {
this.el.querySelector(selector).addEventListener("click", watchFunction((event) => {
let result;
if (this[methods[selector]]) {
let method = this[methods[selector]];
Expand All @@ -460,7 +463,7 @@ const ui = (function () { // eslint-disable-line no-unused-vars
return false;
}
return undefined;
});
}));
});
iframe.document.body.appendChild(this.el);
}
Expand Down
33 changes: 17 additions & 16 deletions addon/data/shooter-interactive-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals console, self, watchFunction, annotatePosition, util, ui, snapping */
/* globals console, self, watchFunction, watchPromise, annotatePosition, util, ui, snapping */
/* globals window, document, location, chromeShooter */

/**********************************************************
Expand Down Expand Up @@ -351,7 +351,7 @@ stateHandlers.crosshairs = {
this.cachedEl = null;
ui.Box.remove();
ui.WholePageOverlay.display(standardOverlayCallbacks);
document.addEventListener("keyup", keyupHandler, false);
document.addEventListener("keyup", watchFunction(keyupHandler), false);
registeredDocumentHandlers.push({name: "keyup", doc: document, handler: keyupHandler});
if (isChrome) {
ui.ChromeInterface.showSaveFullPage();
Expand Down Expand Up @@ -507,7 +507,7 @@ stateHandlers.draggingReady = {

start: function () {
ui.Box.remove();
ui.WholePageOverlay.display(standardOverlayCallbacks, installHandlersOnDocument);
ui.WholePageOverlay.display(standardOverlayCallbacks);
},

mousemove: function (event) {
Expand Down Expand Up @@ -816,9 +816,9 @@ function getScreenPosition() {
function activate() {
ui.Box.remove();
addHandlers();
ui.iframe.display(installHandlersOnDocument).then(() => {
watchPromise(ui.iframe.display(installHandlersOnDocument).then(() => {
setState("crosshairs");
}).catch((err) => {console.error("Error:", err+"", err)});
}));
if (isChrome) {
ui.ChromeInterface.display();
ui.ChromeInterface.onMyShots = function () {
Expand Down Expand Up @@ -879,8 +879,9 @@ function addHandlers() {

function installHandlersOnDocument(docObj) {
for (let [eventName, handler] of primedDocumentHandlers) {
docObj.addEventListener(eventName, handler, eventName !== "keyup");
registeredDocumentHandlers.push({name: eventName, doc: docObj, handler});
let watchHandler = watchFunction(handler);
docObj.addEventListener(eventName, watchHandler, eventName !== "keyup");
registeredDocumentHandlers.push({name: eventName, doc: docObj, watchHandler});
}
}

Expand Down Expand Up @@ -956,27 +957,27 @@ function checkUrl() {
}
}

window.addEventListener("popstate", checkUrl, false);
window.addEventListener("popstate", watchFunction(checkUrl), false);

if (! isChrome) {
self.port.on("isShowing", checkUrl);
self.port.on("isShowing", watchFunction(checkUrl));

self.port.on("destroy", () => {
self.port.on("destroy", watchFunction(() => {
// If we do this in a setTimeout, we get sane error messages.
// If we don't, we get inscruitable ones.
setTimeout(() => {
setTimeout(watchFunction(() => {
deactivate();
self.port.emit("destroyed");
}, 0);
})
}), 0);
}));

// Happens if this worker is detached for some reason
// (such as moving windows, add-on unloading)
self.port.on("detach", () => {
self.port.on("detach", watchFunction(() => {
deactivate();
console.info("detached worker");
});
}));

self.port.emit("ready");
activate();
watchFunction(activate());
}
2 changes: 1 addition & 1 deletion chrome-extension/site-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals document, alert, chrome, setInterval, clearTimeout */
/* globals document, alert, chrome, setInterval, clearTimeout, watchFunction */

function sendEvent(type, detail) {
let event = document.createEvent("CustomEvent");
Expand Down

0 comments on commit f693b9f

Please sign in to comment.