bevy/release-content/migration-guides/labeled_asset_scope_errors.md
andriyDev 723b52abd3
Allow returning an error from labeled_asset_scope. (#19449)
# Objective

- `LoadContext::labeled_asset_scope` cannot return errors back to the
asset loader. This means users that need errors need to fall back to
using the raw `begin_labeled_asset` and `add_loaded_labeled_asset`,
which is more error-prone.

## Solution

- Allow returning a (generic) error from `labeled_asset_scope`.
- This has the unfortunate side effect that closures which don't return
any errors need to A) return Ok at the end, B) need to specify an error
type (e.g., `()`).

---

## Showcase

```rust
// impl AssetLoader for MyLoader
let handle = load_context.labeled_asset_scope("MySubasset", |mut load_context| {
  if !some_precondition {
    return Err(ThingsDontMakeSenseError);
  }
  let handle = load_context.add_labeled_asset("MySubasset/Other", SomeOtherThing(456));
  Ok(Something{ id: 123, handle })
})?;
```
2025-06-04 00:00:32 +00:00

788 B

title: labeled_asset_scope can now return errors. pull_requests: [19449]

labeled_asset_scope now returns a user-specified error type based on their closure. Previously, users would need to fall back to begin_labeled_asset and add_loaded_labeled_asset to handle errors, which is more error-prone. Consider migrating to use labeled_asset_scope if this was you!

However, labeled_asset_scope closures that don't return errors now needs to A) return Ok, and B) specify an error type.

If your code previously looked like this:

labeled_asset_scope(label, |mut load_context| {
  let my_asset = ...;

  my_asset
});

You can migrate it to:

labeled_asset_scope::<_, ()>(label, |mut load_context| {
  let my_asset = ...;

  Ok(my_asset)
}).unwrap();