Fix: call failureCallback if omggif fails to parse GIF in loadImage #8047
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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>