bevy/crates/bevy_asset/src
Austreelis 6db71367d4
allow access to the source error of AssetLoaderError and downcasting (#19471)
# Objective

I have a custom asset loader, and need access to the error it reports
when failing to load (e.g. through `AssetLoadFailedEvent { error:
AssetLoadError::AssetLoaderError(loader_error), .. }`). However
`AssetLoaderError` doesn't expose its `<core::error::Error>::source()`
(i.e. its `error` field. It only formats it when `Display`ed.

*I haven't searched for issues about it.*

## Solution

- Annotate `AssetLoaderError`'s `error` field with `#[source]`.
- Don't include the error when `AssetLoaderError` is `Display`ed (when
one prints an error's source stack like a backtrace, it would now be
dupplicated).
- (optional, included as a separated commit) Add a getter for the `&dyn
Error` stored in the `error` field (whithin an `Arc`). This is more
ergonomic than using `Error::source()` because it casts an `&Arc<dyn
Error>` into an `&dyn Error`, meaning one has to downcast it twice to
get the original error from the loader, including once where you have to
specify the correct type of the *private* `error` field. So downcasting
from `Error::source()` effectively rely on the internal implementation
of `AssetLoaderError`. The getter instead return the trait object
directly, which mean it will directly downcast to the expected loader
error type.

I didn't included a test that checks that double-downcasting
`<AssetLoaderError as Error>::source()` doesn't break user code that
would rely on the private field's type.

## Testing

- Downcasting the trait objects for both `source()` and the `error()`
getter work as described above.
- `cargo test -p bevy_asset --all-features` pass without errors.

---------

Co-authored-by: austreelis <git@swhaele.net>
2025-06-16 21:24:01 +00:00
..
io Fix missing meta files breaking Bevy on itch (#19268) 2025-05-30 20:33:47 +00:00
processor Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
server allow access to the source error of AssetLoaderError and downcasting (#19471) 2025-06-16 21:24:01 +00:00
asset_changed.rs Let query items borrow from query state to avoid needing to clone (#15396) 2025-06-16 21:05:41 +00:00
assets.rs Add get_mut_untracked to Assets (#19487) 2025-06-04 16:34:27 +00:00
direct_access_ext.rs Improve clarity of existing bevy_assets documentation (#17830) 2025-02-13 19:49:25 +00:00
event.rs Event Split: Event, EntityEvent, and BufferedEvent (#19647) 2025-06-15 16:46:34 +00:00
folder.rs Fill out some missing docs for bevy_assets (#17829) 2025-02-13 21:08:09 +00:00
handle.rs Deprecated Begone! 0.16 Cleanup (#19108) 2025-05-07 18:17:41 +00:00
id.rs Added derive Reflect to UntypedHandle and UntypedAssetId (#18827) 2025-04-28 21:46:36 +00:00
lib.rs bevyengine.org -> bevy.org (#19503) 2025-06-05 23:09:28 +00:00
loader_builders.rs Revert PR #15481 to resolve a regression. (#18567) 2025-03-27 21:32:01 +00:00
loader.rs Fix LoadContext docs. (#19610) 2025-06-13 03:48:55 +00:00
meta.rs Harden proc macro path resolution and add integration tests. (#17330) 2025-02-09 19:45:45 +00:00
path.rs Remove apostrophes in possessive its (#19244) 2025-05-26 19:53:14 +00:00
reflect.rs Replace UntypedHandle from ReflectAsset with impl Into<UntypedAssetId>. (#19606) 2025-06-15 16:42:54 +00:00
render_asset.rs bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
saver.rs Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
transformer.rs Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00