p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Improve FES error messages for color functions in p5.strands

Open LalitNarayanYadav opened this issue 5 months ago • 3 comments

Increasing access

Clear error messages help beginners debug shader code. The current FES messages for color functions in strands don't mention the specific function name or the invalid value, making it hard to know what went wrong. The newly added color APIs in p5.strands expose a few cases where Friendly Error System (FES) messages could be more specific and beginner-friendly.

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [x] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [x] Friendly errors
  • [ ] Other (specify if possible)

Feature enhancement details

Three FES message improvements for strands color functions:

  1. Missing parameter — lerpColor(c, 0.5) (missing second color arg): Current: "Function 'mix' has 3 variants which expect 3 arguments, but 2 were provided." Suggested: mention lerpColor by name, not the internal mix function

  2. Wrong parameter type — red("blue") or lerpColor(c1, "blue", 0.5): Current: "You've tried to construct a scalar or vector type with a non-numeric value: blue" Suggested: also name which function was called and which argument was bad

  3. Invalid color string — color('#fv0a'): Current: "Error: Invalid color string" Suggested: include the invalid string in the message so users know what to fix

Related PR: #8822

LalitNarayanYadav avatar Jun 12 '26 19:06 LalitNarayanYadav

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms.

For guidance on contributing, check out our contributing guidelines and other resources for contributors. 💬 If you have questions or need support, feel free to join the Processing Foundation Forum or visit our Community page. 📜 Please also review our Code of Conduct to understand our community standards. Thank You!

welcome[bot] avatar Jun 12 '26 19:06 welcome[bot]

Hi! I'd like to work on this issue.

I traced all three messages to their source:

  1. lerpColor naming mix - lerpColor() in src/strands/strands_api.js forwards its arguments straight to this.mix(...args), so when the argument count is wrong, the arity check in functionCallNode (src/strands/ir_builders.js) reports the internal mix function, not lerpColor.

  2. Generic "non-numeric value" message - red(), green(), blue(), alpha(), hue(), saturation(), brightness(), and lightness() all route through p5.strandsNode() → primitiveConstructorNode → mapPrimitiveDepsToIDs in ir_builders.js. That function has no context about which user-facing function or argument position triggered the error, so it can only report the raw bad value.

  3. "Invalid color string" with no string - thrown in src/color/p5.Color.js. The offending value (vals[0]) is right there in scope when the error is thrown, it's just not included in the message.

My plan:

  • Add an explicit arg-count check inside lerpColor() before it delegates to mix(), so mismatched calls report lerpColor by name (e.g. "lerpColor() expects 3 arguments (color1, color2, amount), but 2 were provided.").
  • Thread an optional context object ({ functionName, argIndex }) through mapPrimitiveDepsToIDs → primitiveConstructorNode → p5.strandsNode, and pass it from the color accessor functions so type errors can name both the function and the argument that caused it.
  • Update the p5.Color.js catch block to include the actual invalid string in the thrown error.
  • Add unit tests covering all three cases (missing arg, wrong type, invalid color string) so regressions are caught going forward.

Could I get this assigned to me? Happy to open a draft PR early for feedback if that's preferred over waiting.

manju2810 avatar Jul 01 '26 09:07 manju2810

Hi @Manju! This looks like a well-researched plan - you've correctly identified all three root causes. Feel free to go ahead and work on this. A draft PR early for feedback sounds like a good approach. You may also want to loop in @davepagurek as he's the primary reviewer for strands-related changes.

LalitNarayanYadav avatar Jul 01 '26 09:07 LalitNarayanYadav