From f32c14da8f8d74587a0cbfad887bfe9a8ca1cec6 Mon Sep 17 00:00:00 2001 From: orph3usLyre <101505114+orph3usLyre@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:41:37 +0100 Subject: [PATCH] `ReadAssetBytesError::Io` exposes failing path (#10450) # Objective Addresses #[10438](https://github.com/bevyengine/bevy/issues/10438) The objective was to include the failing path in the error for the user to see. ## Solution Add a `path` field to the `ReadAssetBytesError::Io` variant to expose the failing path in the error message. ## Migration Guide - The `ReadAssetBytesError::Io` variant now contains two named fields instead of converting from `std::io::Error`. 1. `path`: The requested (failing) path (`PathBuf`) 2. `source`: The source `std::io::Error` --------- Co-authored-by: Alice Cecile --- crates/bevy_asset/src/loader.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/bevy_asset/src/loader.rs b/crates/bevy_asset/src/loader.rs index 4fc451991bbfb..9810e08097736 100644 --- a/crates/bevy_asset/src/loader.rs +++ b/crates/bevy_asset/src/loader.rs @@ -16,7 +16,7 @@ use ron::error::SpannedError; use serde::{Deserialize, Serialize}; use std::{ any::{Any, TypeId}, - path::Path, + path::{Path, PathBuf}, }; use thiserror::Error; @@ -440,7 +440,13 @@ impl<'a> LoadContext<'a> { Default::default() }; let mut bytes = Vec::new(); - reader.read_to_end(&mut bytes).await?; + reader + .read_to_end(&mut bytes) + .await + .map_err(|source| ReadAssetBytesError::Io { + path: path.path().to_path_buf(), + source, + })?; self.loader_dependencies.insert(path.clone_owned(), hash); Ok(bytes) } @@ -570,8 +576,12 @@ pub enum ReadAssetBytesError { #[error(transparent)] MissingProcessedAssetReaderError(#[from] MissingProcessedAssetReaderError), /// Encountered an I/O error while loading an asset. - #[error("Encountered an io error while loading asset: {0}")] - Io(#[from] std::io::Error), + #[error("Encountered an io error while loading asset at `{path}`: {source}")] + Io { + path: PathBuf, + #[source] + source: std::io::Error, + }, #[error("The LoadContext for this read_asset_bytes call requires hash metadata, but it was not provided. This is likely an internal implementation error.")] MissingAssetHash, }