Don't panic on error when loading assets (#1286)
* Don't panic on IO errors * Better formatting for asset server errors
This commit is contained in:
parent
f2b73eaa8a
commit
3d0c4e380c
@ -21,6 +21,7 @@ filesystem_watcher = ["notify"]
|
|||||||
bevy_app = { path = "../bevy_app", version = "0.4.0" }
|
bevy_app = { path = "../bevy_app", version = "0.4.0" }
|
||||||
bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.4.0" }
|
bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.4.0" }
|
||||||
bevy_ecs = { path = "../bevy_ecs", 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_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"] }
|
||||||
bevy_tasks = { path = "../bevy_tasks", version = "0.4.0" }
|
bevy_tasks = { path = "../bevy_tasks", version = "0.4.0" }
|
||||||
bevy_utils = { path = "../bevy_utils", version = "0.4.0" }
|
bevy_utils = { path = "../bevy_utils", version = "0.4.0" }
|
||||||
|
|||||||
@ -6,6 +6,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use bevy_ecs::Res;
|
use bevy_ecs::Res;
|
||||||
|
use bevy_log::warn;
|
||||||
use bevy_tasks::TaskPool;
|
use bevy_tasks::TaskPool;
|
||||||
use bevy_utils::{HashMap, Uuid};
|
use bevy_utils::{HashMap, Uuid};
|
||||||
use crossbeam_channel::TryRecvError;
|
use crossbeam_channel::TryRecvError;
|
||||||
@ -16,16 +17,16 @@ use thiserror::Error;
|
|||||||
/// Errors that occur while loading assets with an AssetServer
|
/// Errors that occur while loading assets with an AssetServer
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum AssetServerError {
|
pub enum AssetServerError {
|
||||||
#[error("asset folder path is not a directory")]
|
#[error("asset folder path is not a directory: {0}")]
|
||||||
AssetFolderNotADirectory(String),
|
AssetFolderNotADirectory(String),
|
||||||
#[error("no AssetLoader found for the given extension")]
|
#[error("no `AssetLoader` found for the given extension: {0:?}")]
|
||||||
MissingAssetLoader(Option<String>),
|
MissingAssetLoader(Option<String>),
|
||||||
#[error("the given type does not match the type of the loaded asset")]
|
#[error("the given type does not match the type of the loaded asset")]
|
||||||
IncorrectHandleType,
|
IncorrectHandleType,
|
||||||
#[error("encountered an error while loading an asset")]
|
#[error("encountered an error while loading an asset: {0}")]
|
||||||
AssetLoaderError(anyhow::Error),
|
AssetLoaderError(anyhow::Error),
|
||||||
#[error("`PathLoader` encountered an error")]
|
#[error("encountered an error while reading an asset: {0}")]
|
||||||
PathLoaderError(#[from] AssetIoError),
|
AssetIoError(#[from] AssetIoError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@ -224,7 +225,17 @@ impl AssetServer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// load the asset bytes
|
// 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
|
// load the asset source using the corresponding AssetLoader
|
||||||
let mut load_context = LoadContext::new(
|
let mut load_context = LoadContext::new(
|
||||||
@ -295,7 +306,9 @@ impl AssetServer {
|
|||||||
self.server
|
self.server
|
||||||
.task_pool
|
.task_pool
|
||||||
.spawn(async move {
|
.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();
|
.detach();
|
||||||
asset_path.into()
|
asset_path.into()
|
||||||
|
|||||||
@ -24,11 +24,11 @@ use thiserror::Error;
|
|||||||
/// Errors that occur while loading assets
|
/// Errors that occur while loading assets
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum AssetIoError {
|
pub enum AssetIoError {
|
||||||
#[error("path not found")]
|
#[error("path not found: {0}")]
|
||||||
NotFound(PathBuf),
|
NotFound(PathBuf),
|
||||||
#[error("encountered an io error while loading asset")]
|
#[error("encountered an io error while loading asset: {0}")]
|
||||||
Io(#[from] io::Error),
|
Io(#[from] io::Error),
|
||||||
#[error("failed to watch path")]
|
#[error("failed to watch path: {0}")]
|
||||||
PathWatchError(PathBuf),
|
PathWatchError(PathBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user