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 <alice.i.cecile@gmail.com>
This commit is contained in:
orph3usLyre 2023-11-16 18:41:37 +01:00 committed by GitHub
parent 3c689b9ca8
commit a2d90a8533
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,
}