Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

theme: Return structured errors when a theme is not found (#25090)

Marshall Bowers8e7cad78488d
diff --git a/Cargo.lock b/Cargo.lock
index 7d504f2e481..52b017b8765 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13481,6 +13481,7 @@ dependencies = [
"serde_repr",
"settings",
"strum",
+ "thiserror 1.0.69",
"util",
"uuid",
]
diff --git a/crates/theme/Cargo.toml b/crates/theme/Cargo.toml
index e0a811335a6..05535eae882 100644
--- a/crates/theme/Cargo.toml
+++ b/crates/theme/Cargo.toml
@@ -36,6 +36,7 @@ serde_json_lenient.workspace = true
serde_repr.workspace = true
settings.workspace = true
strum.workspace = true
+thiserror.workspace = true
util.workspace = true
uuid.workspace = true
diff --git a/crates/theme/src/registry.rs b/crates/theme/src/registry.rs
index f90b6df559a..ed1d232cf5b 100644
--- a/crates/theme/src/registry.rs
+++ b/crates/theme/src/registry.rs
@@ -1,13 +1,14 @@
use std::sync::Arc;
use std::{fmt::Debug, path::Path};
-use anyhow::{anyhow, Context as _, Result};
+use anyhow::{Context as _, Result};
use collections::HashMap;
use derive_more::{Deref, DerefMut};
use fs::Fs;
use futures::StreamExt;
use gpui::{App, AssetSource, Global, SharedString};
use parking_lot::RwLock;
+use thiserror::Error;
use util::ResultExt;
use crate::{
@@ -25,6 +26,16 @@ pub struct ThemeMeta {
pub appearance: Appearance,
}
+/// An error indicating that the theme with the given name was not found.
+#[derive(Debug, Error, Clone)]
+#[error("theme not found: {0}")]
+pub struct ThemeNotFoundError(pub SharedString);
+
+/// An error indicating that the icon theme with the given name was not found.
+#[derive(Debug, Error, Clone)]
+#[error("icon theme not found: {0}")]
+pub struct IconThemeNotFoundError(pub SharedString);
+
/// The global [`ThemeRegistry`].
///
/// This newtype exists for obtaining a unique [`TypeId`](std::any::TypeId) when
@@ -145,12 +156,12 @@ impl ThemeRegistry {
}
/// Returns the theme with the given name.
- pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
+ pub fn get(&self, name: &str) -> Result<Arc<Theme>, ThemeNotFoundError> {
self.state
.read()
.themes
.get(name)
- .ok_or_else(|| anyhow!("theme not found: {}", name))
+ .ok_or_else(|| ThemeNotFoundError(name.to_string().into()))
.cloned()
}
@@ -209,7 +220,7 @@ impl ThemeRegistry {
}
/// Returns the default icon theme.
- pub fn default_icon_theme(&self) -> Result<Arc<IconTheme>> {
+ pub fn default_icon_theme(&self) -> Result<Arc<IconTheme>, IconThemeNotFoundError> {
self.get_icon_theme(DEFAULT_ICON_THEME_NAME)
}
@@ -227,12 +238,12 @@ impl ThemeRegistry {
}
/// Returns the icon theme with the specified name.
- pub fn get_icon_theme(&self, name: &str) -> Result<Arc<IconTheme>> {
+ pub fn get_icon_theme(&self, name: &str) -> Result<Arc<IconTheme>, IconThemeNotFoundError> {
self.state
.read()
.icon_themes
.get(name)
- .ok_or_else(|| anyhow!("icon theme not found: {name}"))
+ .ok_or_else(|| IconThemeNotFoundError(name.to_string().into()))
.cloned()
}
diff --git a/crates/theme/src/settings.rs b/crates/theme/src/settings.rs
index baa2130d856..70e271a108a 100644
--- a/crates/theme/src/settings.rs
+++ b/crates/theme/src/settings.rs
@@ -1,7 +1,7 @@
use crate::fallback_themes::zed_default_dark;
use crate::{
- Appearance, IconTheme, SyntaxTheme, Theme, ThemeRegistry, ThemeStyleContent,
- DEFAULT_ICON_THEME_NAME,
+ Appearance, IconTheme, IconThemeNotFoundError, SyntaxTheme, Theme, ThemeNotFoundError,
+ ThemeRegistry, ThemeStyleContent, DEFAULT_ICON_THEME_NAME,
};
use anyhow::Result;
use derive_more::{Deref, DerefMut};
@@ -578,9 +578,14 @@ impl ThemeSettings {
let mut new_theme = None;
- if let Some(theme) = themes.get(theme).log_err() {
- self.active_theme = theme.clone();
- new_theme = Some(theme);
+ match themes.get(theme) {
+ Ok(theme) => {
+ self.active_theme = theme.clone();
+ new_theme = Some(theme);
+ }
+ Err(err @ ThemeNotFoundError(_)) => {
+ log::error!("{err}");
+ }
}
self.apply_theme_overrides();
@@ -838,8 +843,13 @@ impl settings::Settings for ThemeSettings {
let theme_name = value.theme(*system_appearance);
- if let Some(theme) = themes.get(theme_name).log_err() {
- this.active_theme = theme;
+ match themes.get(theme_name) {
+ Ok(theme) => {
+ this.active_theme = theme;
+ }
+ Err(err @ ThemeNotFoundError(_)) => {
+ log::error!("{err}");
+ }
}
}
@@ -851,8 +861,13 @@ impl settings::Settings for ThemeSettings {
let icon_theme_name = value.icon_theme(*system_appearance);
- if let Some(icon_theme) = themes.get_icon_theme(icon_theme_name).log_err() {
- this.active_icon_theme = icon_theme;
+ match themes.get_icon_theme(icon_theme_name) {
+ Ok(icon_theme) => {
+ this.active_icon_theme = icon_theme;
+ }
+ Err(err @ IconThemeNotFoundError(_)) => {
+ log::error!("{err}");
+ }
}
}