From 22e0c99a29a29b5a6441448af3a7e2643746ad6f Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Fri, 22 Jan 2021 22:01:41 -0500 Subject: [PATCH 1/3] Don't panic on IO errors --- crates/bevy_asset/Cargo.toml | 1 + crates/bevy_asset/src/asset_server.rs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index 21717107236fb..8385655510f4d 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -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" } diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index fafff56008346..99c6ba006ff7b 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -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; @@ -224,7 +225,18 @@ 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::PathLoaderError(err)); + } + }; + // load the asset source using the corresponding AssetLoader let mut load_context = LoadContext::new( @@ -295,7 +307,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!("Asset failed to load: {:?}", err); + } }) .detach(); asset_path.into() From 7cc2237b31b3c4da15030ad8d7e069e8b5673aed Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Sat, 23 Jan 2021 11:24:47 -0500 Subject: [PATCH 2/3] Better formatting for asset server errors --- crates/bevy_asset/src/asset_server.rs | 14 +++++++------- crates/bevy_asset/src/io/mod.rs | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 99c6ba006ff7b..e707e446a15c0 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -17,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), #[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)] @@ -233,7 +233,7 @@ impl AssetServer { .get_mut(&asset_path_id.source_path_id()) .expect("`AssetSource` should exist at this point."); source_info.load_state = LoadState::Failed; - return Err(AssetServerError::PathLoaderError(err)); + return Err(AssetServerError::AssetIoError(err)); } }; @@ -308,7 +308,7 @@ impl AssetServer { .task_pool .spawn(async move { if let Err(err) = server.load_async(owned_path, force).await { - warn!("Asset failed to load: {:?}", err); + warn!("{}", err); } }) .detach(); diff --git a/crates/bevy_asset/src/io/mod.rs b/crates/bevy_asset/src/io/mod.rs index 927c315b39388..d7917972a04a6 100644 --- a/crates/bevy_asset/src/io/mod.rs +++ b/crates/bevy_asset/src/io/mod.rs @@ -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), } From fe171d8943ed868e17895dad2f95400f98b15b4d Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Sat, 23 Jan 2021 11:25:22 -0500 Subject: [PATCH 3/3] Formatting --- crates/bevy_asset/src/asset_server.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index e707e446a15c0..f96a624ef5d91 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -237,7 +237,6 @@ impl AssetServer { } }; - // load the asset source using the corresponding AssetLoader let mut load_context = LoadContext::new( asset_path.path(),