Skip to content

Commit

Permalink
Merge pull request #6244 from davepagurek/fix/internal-errors
Browse files Browse the repository at this point in the history
Update FES check for internal errors to support ES6 classes
  • Loading branch information
limzykenneth authored Jun 29, 2023
2 parents 0729577 + 99830a1 commit 2a1bb03
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/core/friendly_errors/fes_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,22 +497,32 @@ if (typeof IS_MINIFIED !== 'undefined') {
// isInternal - Did this error happen inside the library
let isInternal = false;
let p5FileName, friendlyStack, currentEntryPoint;

// Intentionally throw an error that we catch so that we can check the name
// of the current file. Any errors we see from this file, we treat as
// internal errors.
try {
throw new Error();
} catch (testError) {
const testStacktrace = p5._getErrorStackParser().parse(testError);
p5FileName = testStacktrace[0].fileName;
}

for (let i = stacktrace.length - 1; i >= 0; i--) {
let splitted = stacktrace[i].functionName.split('.');
if (entryPoints.includes(splitted[splitted.length - 1])) {
// remove everything below an entry point function (setup, draw, etc).
// (it's usually the internal initialization calls)
friendlyStack = stacktrace.slice(0, i + 1);
currentEntryPoint = splitted[splitted.length - 1];
for (let j = 0; j < i; j++) {
// Due to the current build process, all p5 functions have
// _main.default in their names in the final build. This is the
// easiest way to check if a function is inside the p5 library
if (stacktrace[j].functionName.search('_main.default') !== -1) {
isInternal = true;
p5FileName = stacktrace[j].fileName;
break;
}
// We call the error "internal" if the source of the error was a
// function from within the p5.js library file, but called from the
// user's code directly. We only need to check the topmost frame in
// the stack trace since any function internal to p5 should pass this
// check, not just public p5 functions.
if (stacktrace[0].fileName === p5FileName) {
isInternal = true;
break;
}
break;
}
Expand Down
22 changes: 22 additions & 0 deletions test/unit/core/error_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,28 @@ suite('Global Error Handling', function() {
});
});

testUnMinified(
'identifies errors happenning internally in ES6 classes',
function() {
return new Promise(function(resolve) {
prepSyntaxTest(
[
'function setup() {',
'let cnv = createCanvas(10, 10, WEBGL);',
'let fbo = createFramebuffer();',
'fbo.draw();', // Error in p5 library as no callback passed
'}'
],
resolve
);
}).then(function() {
assert.strictEqual(log.length, 1);
assert.match(log[0], /inside the p5js library/);
assert.match(log[0], /draw/);
});
}
);

testUnMinified('identifies errors in preload', function() {
return new Promise(function(resolve) {
prepSyntaxTest(
Expand Down

0 comments on commit 2a1bb03

Please sign in to comment.