[p5.js 2.0 Bug Report]: if use min() and max() with Infinity or -Infinity, it throws friendly error
Most appropriate sub-area of p5.js?
- [ ] Accessibility
- [ ] Color
- [ ] Core/Environment/Rendering
- [ ] Data
- [ ] DOM
- [ ] Events
- [ ] Image
- [ ] IO
- [x] Math
- [ ] Typography
- [ ] Utilities
- [ ] WebGL
- [ ] Build process
- [ ] Unit testing
- [ ] Internationalization
- [ ] Friendly errors
- [ ] Other (specify if possible)
p5.js version
2.0.4
Web browser and version
Chrome
Operating system
Windows
Steps to reproduce this
Steps:
- console.log(min(Infinity,3));
- console.log(max(3,-Infinity));
- until 2.0.3, it throws no error. 2.0.4 and 2.0.5 throws friendly error. (There is no problem in getting the value.)
Snippet:
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
console.log(min(Infinity,3));
console.log(max(3,-Infinity));
}
2.0.3
no error.
2.0.4, 2.0.5
friendly error occurs.
p5.disableFriendlyErrors = true;
This might solve the problem, but most users probably don't want to go through the hassle.
A similar problem occurs with p5.Editor, but I was unable to confirm this on the reference site.
This problem doesn't seem to occur when using the library locally also, so I think it's specific to OpenProcessing and p5.Editor.
Ah apparently in zod v4, Infinity stopped being a valid value for a number() validation: https://github.com/colinhacks/zod/issues/4721
Probably need to try updating this line: https://github.com/processing/p5.js/blob/53c12c39e190a16418cc2f05348711976d72c7e4/src/core/friendly_errors/param_validator.js#L67
Something like z.number().or(z.literal(Infinity)) maybe?
Hi @davepagurek I’d like to work on this issue — could you please assign it to me?
Thanks @Nitin2332!
Hi !! @davepagurek if this issue still opens please assign it to me
This issue is open for volunteers again! @ayushman1210 , if you still want to work on this - feel free to go ahead and make the PR. There's a suggestion in a comment above, as well as some previous reviews that may be helpful. Please let us know if there are any questions, we're happy to help! If you don't respond to claim the issue in the next 7 days, it will be open again for volunteers.
Hi !! davepagurekif this issue still opens please assign it to me
Hey @perminder-17 , I was looking into this issue more and went through the related PRs and your suggestion. My understanding is: earlier with plain z.number() the friendly error only showed “Expected number…”. After changing it to z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]), the error builder now sees both number and constant (because +-Infinity are literals), so the message becomes “number or constant…”, which breaks the color() tests. The fix for this is by flattening the union issues and, when both number and constant appear but the only constants are +-Infinity, I’ll remove the constant (please refer to documentation for allowed values) entry from expectedTypes so the message goes back to just “Expected number…”. Please let me know if this matches what you had in mind.
Hey @perminder-17 , I was looking into this issue more and went through the related PRs and your suggestion. My understanding is: earlier with plain z.number() the friendly error only showed “Expected number…”. After changing it to z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]), the error builder now sees both number and constant (because +-Infinity are literals), so the message becomes “number or constant…”, which breaks the color() tests. The fix for this is by flattening the union issues and, when both number and constant appear but the only constants are +-Infinity, I’ll remove the constant (please refer to documentation for allowed values) entry from expectedTypes so the message goes back to just “Expected number…”. Please let me know if this matches what you had in mind.
Yeah, that's the actual idea, yeah. We need to have an exttra condition check.
I know about the fact that the issue is assigned to you, but do you mind if I give it a try once?
I know about the fact that the issue is assigned to you, but do you mind if I give it a try once?
Sure, feel free to give it a try.