Skip to content

Commit

Permalink
Fix FES global error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay-99 committed Aug 8, 2020
1 parent dd7547d commit 46253ee
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
37 changes: 35 additions & 2 deletions src/core/friendly_errors/fes_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 59,9 @@ const ENABLE_FES_STYLING = false;

if (typeof IS_MINIFIED !== 'undefined') {
p5._friendlyError = p5._checkForUserDefinedFunctions = p5._fesErrorMonitor = () => {};
p5._fesErrorWrapper = function() {
this.apply(null, arguments);
};
} else {
let doFriendlyWelcome = false; // TEMP until we get it all working LM

Expand Down Expand Up @@ -409,7 412,7 @@ if (typeof IS_MINIFIED !== 'undefined') {
// and also for detectiong if the error happened inside the library

// cannot process a stacktrace that doesn't exist
if (!stacktrace) return [false, null];
if (!stacktrace || error.name === 'SyntaxError') return [false, null];

stacktrace.forEach(frame => {
frame.functionName = frame.functionName || '';
Expand Down Expand Up @@ -483,6 486,8 @@ if (typeof IS_MINIFIED !== 'undefined') {

// if already handled by another part of the FES, don't handle again
if (p5._fesLogCache[locationObj.location]) return [true, null];

if (locationObj.location) p5._fesLogCache[locationObj.location] = true;
}

// Check if the error is due to a non loadX method being used incorrectly
Expand Down Expand Up @@ -610,6 615,9 @@ if (typeof IS_MINIFIED !== 'undefined') {
file: stacktrace[0].fileName.split('/').slice(-1),
line: friendlyStack[0].lineNumber
};

if (p5._fesLogCache[locationObj.location]) return;
if (locationObj.location) p5._fesLogCache[locationObj.location] = true;
}

switch (error.name) {
Expand Down Expand Up @@ -712,12 720,37 @@ if (typeof IS_MINIFIED !== 'undefined') {
}
};

/**
* Wrapper function to call all user-defined functions.
* (setup, draw, mouseClicked, etc.). Allows fesErrorMonitor to catch errors
* in user-code when the event listener doesn't trigger due to same-origin
* security policies.
*
* @method fesErrorWrapper
* @private
*/
const fesErrorWrapper = function() {
try {
return this.apply(null, arguments);
} catch (err) {
fesErrorMonitor(err);
throw err;
}
};

p5._fesErrorMonitor = fesErrorMonitor;
p5._fesErrorWrapper = fesErrorWrapper;
p5._checkForUserDefinedFunctions = checkForUserDefinedFunctions;

// logger for testing purposes.
// for testing purposes only.
p5._fesLogger = null;
p5._fesLogCache = {};
const clearFESLogCache = function() {
for (const key in p5._fesLogCache) {
delete p5._fesLogCache[key];
}
};
p5._clearFESLogCache = clearFESLogCache;

window.addEventListener('load', checkForUserDefinedFunctions, false);
window.addEventListener('error', p5._fesErrorMonitor, false);
Expand Down
6 changes: 3 additions & 3 deletions src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 268,7 @@ class p5 {
obj[method] = this._wrapPreload(obj, method);
}

userPreload();
p5._fesErrorWrapper.call(userPreload);
this._runIfPreloadsAreDone();
} else {
this._setup();
Expand Down Expand Up @@ -340,7 340,7 @@ class p5 {
// Short-circuit on this, in case someone used the library in "global"
// mode earlier
if (typeof context.setup === 'function') {
context.setup();
p5._fesErrorWrapper.call(context.setup);
}

// unhide any hidden canvases that were created
Expand Down Expand Up @@ -548,7 548,7 @@ class p5 {
for (const e in this._events) {
const f = this[`_on${e}`];
if (f) {
const m = f.bind(this);
const m = p5._fesErrorWrapper.bind(f.bind(this));
window.addEventListener(e, m, { passive: false });
this._events[e] = m;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/p5.Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 813,7 @@ p5.Element._attachListener = function(ev, fxn, ctx) {
if (ctx._events[ev]) {
p5.Element._detachListener(ev, ctx);
}
const f = fxn.bind(ctx);
const f = p5._fesErrorWrapper.bind(fxn.bind(ctx));
ctx.elt.addEventListener(ev, f, false);
ctx._events[ev] = f;
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 484,7 @@ p5.prototype.redraw = function(n) {
context._registeredMethods.pre.forEach(callMethod);
this._inUserDraw = true;
try {
userDraw();
p5._fesErrorWrapper.call(userDraw);
} finally {
this._inUserDraw = false;
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/core/error_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 530,7 @@ suite('Global Error Handling', function() {

teardown(function() {
p5._fesLogger = null;
p5._clearFESLogCache();
});

testUnMinified(
Expand Down

0 comments on commit 46253ee

Please sign in to comment.