rive-app / rive-react

React runtime for Rive
https://rive-app.github.io/rive-react
MIT License
755 stars 27 forks source link

`useRiveFile` does not handle errors thrown in RiveFile constructor #267

Open petersli opened 2 weeks ago

petersli commented 2 weeks ago

I want to use the status returned by the useRiveFile hook to fail gracefully if there is an error while loading the file, e.g. if the file at src is missing.

Currently, if there is an error during the initData method of RiveFile, firing the loadError event is immediately followed by throwing an Error (https://github.com/rive-app/rive-wasm/blob/55691eb4893e3056f2d5096fbc3f2e58142e8d79/js/src/rive.ts#L1453-L1457).

This makes the status returned by useRiveFile provide little value for this use case, since if status === 'failed', the app is going to crash anyways.

This is not an issue when using useRive without useRiveFile, because the call to new RiveFile is wrapped in a try / catch in the Rive constructor (https://github.com/rive-app/rive-wasm/blob/55691eb4893e3056f2d5096fbc3f2e58142e8d79/js/src/rive.ts#L1817-L1824).

I think similarly, useRiveFile should also catch errors thrown by new RiveFile. Without this error handling, I don't think I can use useRiveFile in production.

Other work-arounds also welcome!

Thank you!