Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic on error when loading assets #1286

Merged
merged 3 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ filesystem_watcher = ["notify"]
bevy_app = { path = "../bevy_app", version = "0.4.0" }
bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.4.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" }
bevy_log = { path = "../bevy_log", version = "0.4.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"] }
bevy_tasks = { path = "../bevy_tasks", version = "0.4.0" }
bevy_utils = { path = "../bevy_utils", version = "0.4.0" }
Expand Down
27 changes: 20 additions & 7 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
};
use anyhow::Result;
use bevy_ecs::Res;
use bevy_log::warn;
use bevy_tasks::TaskPool;
use bevy_utils::{HashMap, Uuid};
use crossbeam_channel::TryRecvError;
Expand All @@ -16,16 +17,16 @@ use thiserror::Error;
/// Errors that occur while loading assets with an AssetServer
#[derive(Error, Debug)]
pub enum AssetServerError {
#[error("asset folder path is not a directory")]
#[error("asset folder path is not a directory: {0}")]
AssetFolderNotADirectory(String),
#[error("no AssetLoader found for the given extension")]
#[error("no `AssetLoader` found for the given extension: {0:?}")]
MissingAssetLoader(Option<String>),
#[error("the given type does not match the type of the loaded asset")]
IncorrectHandleType,
#[error("encountered an error while loading an asset")]
#[error("encountered an error while loading an asset: {0}")]
AssetLoaderError(anyhow::Error),
#[error("`PathLoader` encountered an error")]
PathLoaderError(#[from] AssetIoError),
#[error("encountered an error while reading an asset: {0}")]
AssetIoError(#[from] AssetIoError),
}

#[derive(Default)]
Expand Down Expand Up @@ -224,7 +225,17 @@ impl AssetServer {
};

// load the asset bytes
let bytes = self.server.asset_io.load_path(asset_path.path()).await?;
let bytes = match self.server.asset_io.load_path(asset_path.path()).await {
Ok(bytes) => bytes,
Err(err) => {
let mut asset_sources = self.server.asset_sources.write();
let source_info = asset_sources
.get_mut(&asset_path_id.source_path_id())
.expect("`AssetSource` should exist at this point.");
source_info.load_state = LoadState::Failed;
return Err(AssetServerError::AssetIoError(err));
}
};

// load the asset source using the corresponding AssetLoader
let mut load_context = LoadContext::new(
Expand Down Expand Up @@ -295,7 +306,9 @@ impl AssetServer {
self.server
.task_pool
.spawn(async move {
server.load_async(owned_path, force).await.unwrap();
if let Err(err) = server.load_async(owned_path, force).await {
warn!("{}", err);
}
})
.detach();
asset_path.into()
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use thiserror::Error;
/// Errors that occur while loading assets
#[derive(Error, Debug)]
pub enum AssetIoError {
#[error("path not found")]
#[error("path not found: {0}")]
NotFound(PathBuf),
#[error("encountered an io error while loading asset")]
#[error("encountered an io error while loading asset: {0}")]
Io(#[from] io::Error),
#[error("failed to watch path")]
#[error("failed to watch path: {0}")]
PathWatchError(PathBuf),
}

Expand Down