Skip to content

Provide help for global APIs used at top-level code. #1130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/core/error_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,38 @@ function friendlyWelcome() {
report(str, 'println', '#4DB200'); // auto dark green
} */

// This is a list of p5 functions/variables that are commonly misused
// by beginners at top-level code, outside of setup/draw. We'd like to
// detect these errors and help the user by suggesting they move them
// into setup/draw.
//
// For more details, see https://github.com/processing/p5.js/issues/1121.
var misusedAtTopLevelCode = [
'color',
'random'
];

function helpForMisusedAtTopLevelCode(e) {
misusedAtTopLevelCode.forEach(function(name) {
if (e.message && e.message.indexOf(name) !== -1) {
console.log('%c Did you just try to use p5.js\'s \'' + name + '\' ' +
'function or variable? If so, you may want to ' +
'move it into your sketch\'s setup() function.',
'color: #B40033' /* Dark magenta */);
}
});
}

if (document.readyState !== 'complete') {
window.addEventListener('error', helpForMisusedAtTopLevelCode, false);

// Our job is only to catch ReferenceErrors that are thrown when
// global (non-instance mode) p5 APIs are used at the top-level
// scope of a file, so we'll unbind our error listener now to make
// sure we don't log false positives later.
window.addEventListener('load', function() {
window.removeEventListener('error', helpForMisusedAtTopLevelCode, false);
});
}

module.exports = p5;