Skip to content

Commit

Permalink
Improve image loading error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 14, 2023
1 parent 539e657 commit 4e2a0b8
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
18 changes: 6 additions & 12 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,15 +1921,9 @@ impl Context {
pub fn is_loader_installed(&self, id: &str) -> bool {
let loaders = self.loaders();

let in_bytes = loaders.bytes.lock().iter().any(|loader| loader.id() == id);
let in_image = loaders.image.lock().iter().any(|loader| loader.id() == id);
let in_texture = loaders
.texture
.lock()
.iter()
.any(|loader| loader.id() == id);

in_bytes || in_image || in_texture
loaders.bytes.lock().iter().any(|l| l.id() == id)
|| loaders.image.lock().iter().any(|l| l.id() == id)
|| loaders.texture.lock().iter().any(|l| l.id() == id)
}

/// Append an entry onto the chain of bytes loaders.
Expand Down Expand Up @@ -2028,7 +2022,7 @@ impl Context {
}
}

Err(load::LoadError::NotSupported)
Err(load::LoadError::NoMatchingBytesLoader)
}

/// Try loading the image from the given uri using any available image loaders.
Expand Down Expand Up @@ -2067,7 +2061,7 @@ impl Context {
}
}

Err(load::LoadError::NotSupported)
Err(load::LoadError::NoMatchingImageLoader)
}

/// Try loading the texture from the given uri using any available texture loaders.
Expand Down Expand Up @@ -2103,7 +2097,7 @@ impl Context {
}
}

Err(load::LoadError::NotSupported)
Err(load::LoadError::NoMatchingTextureLoader)
}

/// The loaders of bytes, images, and textures.
Expand Down
34 changes: 27 additions & 7 deletions crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,44 @@ use std::{error::Error as StdError, fmt::Display, sync::Arc};
/// Represents a failed attempt at loading an image.
#[derive(Clone, Debug)]
pub enum LoadError {
/// There are no image loaders installed.
/// Programmer error: There are no image loaders installed.
NoImageLoaders,

/// This loader does not support this schema, protocol or image format.
/// A specific loader does not support this schema, protocol or image format.
NotSupported,

/// A custom error message (e.g. "File not found: foo.png").
Custom(String),
/// Programmer error: Failed to find the bytes for this image because
/// there was no [`BytesLoader`] supporting the schema.
NoMatchingBytesLoader,

/// Programmer error: Failed to parse the bytes as an image because
/// there was no [`ImageLoader`] supporting the schema.
NoMatchingImageLoader,

/// Programmer error: no matching [`TextureLoader`].
/// Because of the [`DefaultTextureLoader`], this error should never happen.
NoMatchingTextureLoader,

/// Runtime error: Loading was attempted, but failed (e.g. "File not found").
Loading(String),
}

impl Display for LoadError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LoadError::NoImageLoaders => f.write_str(
Self::NoImageLoaders => f.write_str(
"No image loaders are installed. If you're trying to load some images \
for the first time, follow the steps outlined in https://docs.rs/egui/latest/egui/load/index.html"),
LoadError::NotSupported => f.write_str("not supported"),
LoadError::Custom(message) => f.write_str(message),

Self::NoMatchingBytesLoader => f.write_str("No matching BytesLoader. Either you need to call Context::include_bytes, or install some more bytes loaders, e.g. using egui_extras."),

Self::NoMatchingImageLoader => f.write_str("No matching ImageLoader. Either you need to call Context::include_bytes, or install some more bytes loaders, e.g. using egui_extras."),

Self::NoMatchingTextureLoader => f.write_str("No matching TextureLoader. Did you remove the default one?"),

Self::NotSupported => f.write_str("Iagge schema or URI not supported by this loader"),

Self::Loading(message) => f.write_str(message),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_extras/src/loaders/ehttp_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl BytesLoader for EhttpLoader {
bytes: Bytes::Shared(file.bytes),
mime: file.mime,
}),
Poll::Ready(Err(err)) => Err(LoadError::Custom(err)),
Poll::Ready(Err(err)) => Err(LoadError::Loading(err)),
Poll::Pending => Ok(BytesPoll::Pending { size: None }),
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_extras/src/loaders/file_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl BytesLoader for FileLoader {
bytes: Bytes::Shared(file.bytes),
mime: file.mime,
}),
Poll::Ready(Err(err)) => Err(LoadError::Custom(err)),
Poll::Ready(Err(err)) => Err(LoadError::Loading(err)),
Poll::Pending => Ok(BytesPoll::Pending { size: None }),
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_extras/src/loaders/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ImageLoader for ImageCrateLoader {
if let Some(entry) = cache.get(uri).cloned() {
match entry {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Custom(err)),
Err(err) => Err(LoadError::Loading(err)),
}
} else {
match ctx.try_load_bytes(uri) {
Expand All @@ -68,7 +68,7 @@ impl ImageLoader for ImageCrateLoader {
cache.insert(uri.into(), result.clone());
match result {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Custom(err)),
Err(err) => Err(LoadError::Loading(err)),
}
}
Ok(BytesPoll::Pending { size }) => Ok(ImagePoll::Pending { size }),
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_extras/src/loaders/svg_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ImageLoader for SvgLoader {
if let Some(entry) = cache.get(&(uri.clone(), size_hint)).cloned() {
match entry {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Custom(err)),
Err(err) => Err(LoadError::Loading(err)),
}
} else {
match ctx.try_load_bytes(&uri) {
Expand All @@ -60,7 +60,7 @@ impl ImageLoader for SvgLoader {
cache.insert((uri, size_hint), result.clone());
match result {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Custom(err)),
Err(err) => Err(LoadError::Loading(err)),
}
}
Ok(BytesPoll::Pending { size }) => Ok(ImagePoll::Pending { size }),
Expand Down

0 comments on commit 4e2a0b8

Please sign in to comment.