Skip to content

Commit d59f69b

Browse files
committed
Handle promise passed where one is not expected
1 parent 5bd348a commit d59f69b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/core/friendly_errors/param_validator.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function validateParams(p5, fn, lifecycles) {
352352
* @param {String} func - Name of the function. Expect global functions like `sin` and class methods like `p5.Vector.add`
353353
* @returns {String} The friendly error message.
354354
*/
355-
fn.friendlyParamError = function (zodErrorObj, func) {
355+
fn.friendlyParamError = function (zodErrorObj, func, args) {
356356
let message = '🌸 p5.js says: ';
357357
// The `zodErrorObj` might contain multiple errors of equal importance
358358
// (after scoring the schema closeness in `findClosestSchema`). Here, we
@@ -398,6 +398,11 @@ function validateParams(p5, fn, lifecycles) {
398398
});
399399

400400
if (expectedTypes.size > 0) {
401+
if (error.path?.length > 0 && args[error.path[0]] instanceof Promise) {
402+
message += 'Did you mean to put `await` before a loading function? ' +
403+
'An unexpected Promise was found. ';
404+
}
405+
401406
const expectedTypesStr = Array.from(expectedTypes).join(' or ');
402407
const position = error.path.join('.');
403408

@@ -502,7 +507,7 @@ function validateParams(p5, fn, lifecycles) {
502507
} catch (error) {
503508
const closestSchema = fn.findClosestSchema(funcSchemas, args);
504509
const zodError = closestSchema.safeParse(args).error;
505-
const errorMessage = fn.friendlyParamError(zodError, func);
510+
const errorMessage = fn.friendlyParamError(zodError, func, args);
506511

507512
return {
508513
success: false,

test/unit/core/param_errors.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ suite('Validate Params', function () {
77
Color: function () {
88
return 'mock p5.Color';
99
},
10+
Image: function() {
11+
return 'mock p5.Image';
12+
},
13+
Element: function() {
14+
return 'mock p5.Element';
15+
},
16+
Texture: function() {
17+
return 'mock p5.Texture';
18+
},
19+
Framebuffer: function() {
20+
return 'mock p5.Framebuffer';
21+
},
22+
FramebufferTexture: function() {
23+
return 'mock p5.FramebufferTexture';
24+
},
1025
};
1126
const mockP5Prototype = {};
1227

@@ -102,6 +117,17 @@ suite('Validate Params', function () {
102117
});
103118
});
104119

120+
suite('validateParams: promise where no promise is expected', function () {
121+
test('image(): promise for first argument', function () {
122+
const result = mockP5Prototype.validate('p5.image', [Promise.resolve(), 0, 0]);
123+
console.log(result);
124+
assert.equal(
125+
result.error,
126+
'🌸 p5.js says: Did you mean to put `await` before a loading function? An unexpected Promise was found. Expected Image or Element or Texture or Framebuffer or FramebufferTexture at the first parameter in p5.image().'
127+
);
128+
});
129+
});
130+
105131
suite('validateParams: class, multi-types + optional numbers', function () {
106132
test('ambientLight(): no firendly-err-msg', function () {
107133
const result = mockP5Prototype.validate('p5.ambientLight', [new mockP5.Color()]);

0 commit comments

Comments
 (0)