Skip to content

[p5.js 2.0 Bug Report]: Friendly errors do not prevent runtime exceptions in color APIs #8458

@shuklaaryan367-byte

Description

@shuklaaryan367-byte

Most appropriate sub-area of p5.js?

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

p5.js version

2.2.0

Web browser and version

Google Chrome Version 144.0.7559.61

Operating system

Windows 11

Steps to reproduce this

Description:
While testing p5.js 2.0 color-related functions, I noticed that several APIs emit a Friendly Error but still throw runtime exceptions (e.g. TypeError or uncaught Error), causing sketches to crash.

In p5.js, a Friendly Error is generally expected to safely handle invalid input without crashing execution. However, this behaviour is inconsistent across color APIs.

Steps:

  1. Run the sketch given below
  2. Observe that it throws an FES error but still sketch crashes

Snippet:

function setup() {
  noCanvas();

  let c1 = color(255, 0, 0);
  let c2 = color(0, 0, 255);

  // Case 1: lerpColor crashes after friendly error
  console.log(lerpColor(null, c2, 0.5));

  // Case 2: invalid color string crashes
  background("");
}

Observed Behaviour

  1. Friendly Error is logged
  2. Sketch then crashes with:
  • TypeError: Cannot read properties of null
  • or Error: Invalid color string

Expected Behaviour

Friendly Error should:

  1. clearly report invalid input
  2. prevent runtime exceptions
  3. allow sketch execution to continue safely

Additional Notes
This pattern occurs in multiple color-related APIs (background, fill, stroke, color, alpha, lerpColor). Some functions correctly handle invalid input after emitting a Friendly Error, while others still throw.

I'm willing to implement a fix if this gets approved

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions