2
2
* @for p5
3
3
* @requires core
4
4
*
5
- * This is the main file for the Friendly Error System (FES). Here is a
5
+ * This is the main file for the Friendly Error System (FES), containing
6
+ * the core as well as miscellaneous functionality of the FES. Here is a
6
7
* brief outline of the functions called in this system.
7
8
*
8
- * The FES may be invoked by a call to either (1) _validateParameters,
9
- * (2) _friendlyFileLoadError , (3) _friendlyError , (4) helpForMisusedAtTopLevelCode ,
10
- * or (5) _fesErrorMontitor.
9
+ * The FES may be invoked by a call to either
10
+ * (1) _validateParameters , (2) _friendlyFileLoadError , (3) _friendlyError ,
11
+ * (4) helpForMisusedAtTopLevelCode, or (5) _fesErrorMontitor.
11
12
*
12
- * _validateParameters is located in validate_params.js along with other code used
13
- * for parameter validation.
14
- * _friendlyFileLoadError is located in file_errors.js along with other code used for
15
- * dealing with file load errors.
16
- * Apart from this, there's also a file stacktrace.js, which contains the code to parse
17
- * the error stack, borrowed from https://github.com/stacktracejs/stacktrace.js
13
+ * _validateParameters is located in validate_params.js along with other code
14
+ * used for parameter validation.
15
+ * _friendlyFileLoadError is located in file_errors.js along with other code
16
+ * used for dealing with file load errors.
17
+ * Apart from this, there's also a file stacktrace.js, which contains the code
18
+ * to parse the error stack, borrowed from:
19
+ * https://github.com/stacktracejs/stacktrace.js
18
20
*
19
- * This file contains the core as well as miscellaneous functionality of the FES.
20
- *
21
- * helpForMisusedAtTopLevelCode is called by this file on window load to check for use
22
- * of p5.js functions outside of setup() or draw()
23
- * Items 1-3 above are called by functions in the p5 library located in other files.
24
- *
25
- * _fesErrorMonitor can be called either by an error event, an unhandled rejection event
26
- * or it can be manually called in a catch block as follows:
27
- * try { someCode(); } catch(err) { p5._fesErrorMonitor(err); }
28
- * fesErrorMonitor is responsible for handling all kinds of errors that the browser may show.
29
- * It gives a simplified explanation for these. It currently works with some kinds of
30
- * ReferenceError, SyntaxError, and TypeError. The complete list of supported errors can be
31
- * found in browser_errors.js.
32
- *
33
- * _friendlyFileLoadError is called by the loadX() methods.
34
- * _friendlyError can be called by any function to offer a helpful error message.
35
- *
36
- * _validateParameters is called by functions in the p5.js API to help users ensure
37
- * ther are calling p5 function with the right parameter types. The property
38
- * disableFriendlyErrors = false can be set from a p5.js sketch to turn off parameter
39
- * checking. The call sequence from _validateParameters looks something like this:
40
- *
41
- * _validateParameters
42
- * buildArgTypeCache
43
- * addType
44
- * lookupParamDoc
45
- * scoreOverload
46
- * testParamTypes
47
- * testParamType
48
- * getOverloadErrors
49
- * _friendlyParamError
50
- * ValidationError
51
- * report
52
- * friendlyWelcome
53
- *
54
- * The call sequences to _friendlyFileLoadError and _friendlyError are like this:
55
- * _friendlyFileLoadError
56
- * report
57
- *
58
- * _friendlyError
59
- * report
60
- *
61
- * The call sequence for _fesErrorMonitor roughly looks something like:
62
- * _fesErrorMonitor
63
- * processStack
64
- * printFriendlyError
65
- * (if type of error is ReferenceError)
66
- * _handleMisspelling
67
- * computeEditDistance
68
- * report
69
- * report
70
- * printFriendlyStack
71
- * (if type of error is SyntaxError, TypeError, etc)
72
- * report
73
- * printFriendlyStack
74
- *
75
- * report() is the main function that prints directly to console with the output
76
- * of the error helper message. Note: friendlyWelcome() also prints to console directly.
21
+ * For more detailed information on the FES functions, including the call
22
+ * sequence of each function, please look at the FES Reference + Dev Notes:
23
+ * https://github.com/processing/p5.js/blob/main/contributor_docs/fes_reference_dev_notes.md
77
24
*/
78
25
import p5 from '../main' ;
79
26
import { translator } from '../internationalization' ;
@@ -176,8 +123,8 @@ if (typeof IS_MINIFIED !== 'undefined') {
176
123
*
177
124
* @method mapToReference
178
125
* @private
179
- * @param {String } message the words to be said
180
- * @param {String } [func] the name of the function to link
126
+ * @param {String } message the words to be said
127
+ * @param {String } [func] the name of function
181
128
*
182
129
* @returns {String }
183
130
*/
@@ -199,12 +146,13 @@ if (typeof IS_MINIFIED !== 'undefined') {
199
146
200
147
/**
201
148
* Prints out a fancy, colorful message to the console log
149
+ * Attaches Friendly Errors prefix [fes.pre] to the message.
202
150
*
203
151
* @method _report
204
152
* @private
205
- * @param {String } message the words to be said
206
- * @param {String } [func] the name of the function to link
207
- * @param {Number|String } [color] CSS color string or error type
153
+ * @param {String } message Message to be printed
154
+ * @param {String } [func] Name of function
155
+ * @param {Number|String } [color] CSS color code
208
156
*
209
157
* @return console logs
210
158
*/
@@ -242,16 +190,17 @@ if (typeof IS_MINIFIED !== 'undefined') {
242
190
*
243
191
* @method _friendlyError
244
192
* @private
245
- * @param {Number } message message to be printed
246
- * @param {String } [method] name of method
247
- * @param {Number|String } [color] CSS color string or error type
193
+ * @param {String } message Message to be printed
194
+ * @param {String } [func] Name of the function linked to error
195
+ * @param {Number|String } [color] CSS color code
248
196
*/
249
- p5 . _friendlyError = function ( message , method , color ) {
250
- p5 . _report ( message , method , color ) ;
197
+ p5 . _friendlyError = function ( message , func , color ) {
198
+ p5 . _report ( message , func , color ) ;
251
199
} ;
252
200
253
201
/**
254
- * This is called internally if there is a error with autoplay.
202
+ * This is called internally if there is an error with autoplay. Generates
203
+ * and prints a friendly error message [fes.autoplay].
255
204
*
256
205
* @method _friendlyAutoplayError
257
206
* @private
@@ -265,11 +214,13 @@ if (typeof IS_MINIFIED !== 'undefined') {
265
214
} ;
266
215
267
216
/**
268
- * An implementation of
269
- * https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm to
270
- * compute the Levenshtein distance. It gives a measure of how dissimilar
271
- * two strings are. If the "distance" between them is small enough, it is
217
+ * Measures dissimilarity between two strings by calculating
218
+ * the Levenshtein distance.
219
+ *
220
+ * If the "distance" between them is small enough, it is
272
221
* reasonable to think that one is the misspelled version of the other.
222
+ *
223
+ * Specifically, this uses the Wagner–Fischer algorithm.
273
224
* @method computeEditDistance
274
225
* @private
275
226
* @param {String } w1 the first word
@@ -316,12 +267,15 @@ if (typeof IS_MINIFIED !== 'undefined') {
316
267
} ;
317
268
318
269
/**
319
- * checks if the various functions such as setup, draw, preload have been
320
- * defined with capitalization mistakes
270
+ * Checks capitalization for user defined functions.
271
+ *
272
+ * Generates and prints a friendly error message using key:
273
+ * "fes.checkUserDefinedFns".
274
+ *
321
275
* @method checkForUserDefinedFunctions
322
276
* @private
323
- * @param {* } context The current default context. It's set to window in
324
- * "global mode" and to a p5 instance in "instance mode"
277
+ * @param {* } context Current default context. Set to window in
278
+ * "global mode" and to a p5 instance in "instance mode"
325
279
*/
326
280
const checkForUserDefinedFunctions = context => {
327
281
if ( p5 . disableFriendlyErrors ) return ;
@@ -360,19 +314,17 @@ if (typeof IS_MINIFIED !== 'undefined') {
360
314
} ;
361
315
362
316
/**
363
- * compares the the symbol caught in the ReferenceErrror to everything
364
- * in misusedAtTopLevel ( all public p5 properties ). The use of
365
- * misusedAtTopLevel here is for convenience as it was an array that was
366
- * already defined when spelling check was implemented. For this particular
367
- * use-case, it's a misnomer.
317
+ * Compares the symbol caught in the ReferenceErrror to everything in
318
+ * misusedAtTopLevel ( all public p5 properties ).
319
+ *
320
+ * Generates and prints a friendly error message using key: "fes.misspelling".
368
321
*
369
322
* @method handleMisspelling
370
323
* @private
371
- * @param {String } errSym the symbol to whose spelling to check
372
- * @param {Error } error the ReferenceError object
324
+ * @param {String } errSym Symbol to whose spelling to check
325
+ * @param {Error } error ReferenceError object
373
326
*
374
- * @returns {Boolean } a boolean value indicating if this error was likely due
375
- * to a mis-spelling
327
+ * @returns {Boolean } tell whether error was likely due to typo
376
328
*/
377
329
const handleMisspelling = ( errSym , error ) => {
378
330
if ( ! misusedAtTopLevelCode ) {
@@ -422,7 +374,7 @@ if (typeof IS_MINIFIED !== 'undefined') {
422
374
if ( matchedSymbols . length === 1 ) {
423
375
// To be used when there is only one closest match. The count parameter
424
376
// allows i18n to pick between the keys "fes.misspelling" and
425
- // "fes.misspelling__plural "
377
+ // "fes.misspelling_plural "
426
378
msg = translator ( 'fes.misspelling' , {
427
379
name : errSym ,
428
380
actualName : matchedSymbols [ 0 ] . name ,
@@ -464,8 +416,11 @@ if (typeof IS_MINIFIED !== 'undefined') {
464
416
} ;
465
417
466
418
/**
467
- * prints a friendly stacktrace which only includes user-written functions
468
- * and is easier for newcomers to understand
419
+ * Prints a friendly stacktrace for user-written functions for "global" errors
420
+ *
421
+ * Generates and prints a friendly error message using key:
422
+ * "fes.globalErrors.stackTop", "fes.globalErrors.stackSubseq".
423
+ *
469
424
* @method printFriendlyStack
470
425
* @private
471
426
* @param {Array } friendlyStack
@@ -501,21 +456,28 @@ if (typeof IS_MINIFIED !== 'undefined') {
501
456
502
457
/**
503
458
* Takes a stacktrace array and filters out all frames that show internal p5
504
- * details. It also uses this processed stack to figure out if the error
505
- * error happened internally within the library, and if the error was due to
506
- * a non-loadX() method being used in preload
507
- * "Internally" here means that the error exact location of the error (the
508
- * top of the stack) is a piece of code write in the p5.js library (which may
509
- * or may not have been called from the user's sketch)
459
+ * details.
460
+ *
461
+ * Generates and prints a friendly error message using key:
462
+ * "fes.wrongPreload", "fes.libraryError".
463
+ *
464
+ * The processed stack is used to find whether the error happended internally
465
+ * within the library, and if the error was due to a non-loadX() method
466
+ * being used in preload.
467
+ *
468
+ * "Internally" here means that the exact location of the error (the top of
469
+ * the stack) is a piece of code write in the p5.js library (which may or
470
+ * may not have been called from the user's sketch).
510
471
*
511
472
* @method processStack
512
473
* @private
513
474
* @param {Error } error
514
475
* @param {Array } stacktrace
515
476
*
516
477
* @returns {Array } An array with two elements, [isInternal, friendlyStack]
517
- * isInternal: a boolean indicating if the error happened internally
518
- * friendlyStack: the simplified stacktrace, with internal details filtered
478
+ * isInternal: a boolean value indicating whether the error
479
+ * happened internally
480
+ * friendlyStack: the filtered (simplified) stacktrace
519
481
*/
520
482
const processStack = ( error , stacktrace ) => {
521
483
// cannot process a stacktrace that doesn't exist
@@ -635,13 +597,17 @@ if (typeof IS_MINIFIED !== 'undefined') {
635
597
} ;
636
598
637
599
/**
638
- * The main function for handling global errors. Called when an error
639
- * happens and is responsible for detecting the type of error that
640
- * has happened and showing the appropriate message
600
+ * Handles "global" errors that the browser catches.
601
+ *
602
+ * Called when an error event happens and detects the type of error.
603
+ *
604
+ * Generates and prints a friendly error message using key:
605
+ * "fes.globalErrors.syntax.[*]", "fes.globalErrors.reference.[*]",
606
+ * "fes.globalErrors.type.[*]".
641
607
*
642
608
* @method fesErrorMonitor
643
609
* @private
644
- * @param {* } e The object to extract error details from
610
+ * @param {* } e Event object to extract error details from
645
611
*/
646
612
const fesErrorMonitor = e => {
647
613
if ( p5 . disableFriendlyErrors ) return ;
@@ -986,6 +952,12 @@ misusedAtTopLevelCode = null;
986
952
const FAQ_URL =
987
953
'https://github.com/processing/p5.js/wiki/p5.js-overview#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup' ;
988
954
955
+ /**
956
+ * A helper function for populating misusedAtTopLevel list.
957
+ *
958
+ * @method defineMisusedAtTopLevelCode
959
+ * @private
960
+ */
989
961
defineMisusedAtTopLevelCode = ( ) => {
990
962
const uniqueNamesFound = { } ;
991
963
@@ -1031,6 +1003,20 @@ defineMisusedAtTopLevelCode = () => {
1031
1003
misusedAtTopLevelCode . sort ( ( a , b ) => b . name . length - a . name . length ) ;
1032
1004
} ;
1033
1005
1006
+ /**
1007
+ * Detects browser level error event for p5 constants/functions used outside
1008
+ * of setup() and draw().
1009
+ *
1010
+ * Generates and prints a friendly error message using key:
1011
+ * "fes.misusedTopLevel".
1012
+ *
1013
+ * @method helpForMisusedAtTopLevelCode
1014
+ * @private
1015
+ * @param {Event } e Error event
1016
+ * @param {Boolean } log false
1017
+ *
1018
+ * @returns {Boolean } true
1019
+ */
1034
1020
const helpForMisusedAtTopLevelCode = ( e , log ) => {
1035
1021
if ( ! log ) {
1036
1022
log = console . log . bind ( console ) ;
0 commit comments