Skip to content

Conversation

Satish-Medar
Copy link

Resolves #8021

Changes:

Wrapped the omggif.GifReader constructor in a try/catch block in loading_displaying.js.
If a GIF cannot be parsed, the failureCallback of loadImage is now called with the error, instead of throwing an uncatchable exception.
This allows sketches to handle image loading errors gracefully and prevents the sketch from crashing.
Tested locally with a broken GIF using a simple HTML test file (test-gif-error.html).
Screenshots of the change:
Screenshot 2025-08-24 111218

When loading a broken GIF, the failure callback is called and the sketch continues running (see console and page output in the test file).

PR Checklist
npm run lint passes
Inline reference is included / updated (not applicable)
Unit tests are included / updated (manual test provided)

test-gif-error.html ( p5.js --> create [ test-gif-error.html ] )

code in [[ test-gif-error.html ]]

<script src="lib/p5.js"></script> <script> function preload() { loadImage( "broken.gif", // Use a GIF that omggif cannot parse (img) => console.log("Loaded!", img), (err) => { console.error("Properly handled error:", err); document.body.innerHTML += "

Failure callback called!

"; } ); } function setup() { createCanvas(100, 100); background(200); } </script>

Copy link

welcome bot commented Aug 24, 2025

🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors!
🤔 Please ensure that your PR links to an issue, which has been approved for work by a maintainer; otherwise, there might already be someone working on it, or still ongoing discussion about implementation. You are welcome to join the discussion in an Issue if you're not sure!
🌸 Once your PR is merged, be sure to add yourself to the list of contributors on the readme page !

Thank You!

@ThomasFinnern
Copy link

is calling "failureCallback(e); on failing gif ... safe ? Or does it enable a possible call of a not wanted function.
Just an offside comment with good intention. I am sorry if it is on the wrong place here.

@Satish-Medar
Copy link
Author

@ThomasFinnern Thanks for raising this! failureCallback here is user-supplied (similar to the success callback), so calling it with the error object is consistent with how other loading functions behave in p5.js. It shouldn’t allow unwanted function calls since the user has to explicitly pass the callback. Still, I appreciate the point — good to double-check!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

loadImage with GIFs that can't be parsed throws an error that breaks the sketch
3 participants