Skip to content

Commit

Permalink
Don't panic on error when loading assets (bevyengine#1286)
Browse files Browse the repository at this point in the history
* Don't panic on IO errors
* Better formatting for asset server errors
  • Loading branch information
willcrichton authored and rparrett committed Jan 27, 2021
1 parent 63ec487 commit 9ddcc83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
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

0 comments on commit 9ddcc83

Please sign in to comment.