Merge pull request #1880 from Andrew15-5/improve-cli-errors

refactor(cli): added multiple nested errors support
This commit is contained in:
ealmloff authored and GitHub committed 2024-01-30 09:59:00 -06:00
commit c518c91c4c
1 file changed
+26 -24
+26 -24
View File
@@ -1,7 +1,7 @@
use dioxus_cli_config::DioxusConfig; use dioxus_cli_config::DioxusConfig;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::anyhow; use anyhow::Context;
use clap::Parser; use clap::Parser;
use dioxus_cli::*; use dioxus_cli::*;
@@ -19,24 +19,26 @@ fn get_bin(bin: Option<String>) -> Result<PathBuf> {
.workspace_packages() .workspace_packages()
.into_iter() .into_iter()
.find(|p| p.name == bin) .find(|p| p.name == bin)
.ok_or(format!("no such package: {}", bin)) .ok_or(Error::CargoError(format!("no such package: {}", bin)))?
.map_err(Error::CargoError)?
} else { } else {
metadata metadata
.root_package() .root_package()
.ok_or("no root package?".into()) .ok_or(Error::CargoError("no root package?".to_string()))?
.map_err(Error::CargoError)?
}; };
let crate_dir = package let crate_dir = package
.manifest_path .manifest_path
.parent() .parent()
.ok_or("couldn't take parent dir".into()) .ok_or(Error::CargoError("couldn't take parent dir".to_string()))?;
.map_err(Error::CargoError)?;
Ok(crate_dir.into()) Ok(crate_dir.into())
} }
/// Simplifies error messages that use the same pattern.
fn error_wrapper(message: &str) -> String {
format!("🚫 {message}:")
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let args = Cli::parse(); let args = Cli::parse();
@@ -46,35 +48,35 @@ async fn main() -> anyhow::Result<()> {
match args.action { match args.action {
Translate(opts) => opts Translate(opts) => opts
.translate() .translate()
.map_err(|e| anyhow!("🚫 Translation of HTML into RSX failed: {}", e)), .context(error_wrapper("Translation of HTML into RSX failed")),
Create(opts) => opts Create(opts) => opts
.create() .create()
.map_err(|e| anyhow!("🚫 Creating new project failed: {}", e)), .context(error_wrapper("Creating new project failed")),
Init(opts) => opts Init(opts) => opts
.init() .init()
.map_err(|e| anyhow!("🚫 Initialising a new project failed: {}", e)), .context(error_wrapper("Initialising a new project failed")),
Config(opts) => opts Config(opts) => opts
.config() .config()
.map_err(|e| anyhow!("🚫 Configuring new project failed: {}", e)), .context(error_wrapper("Configuring new project failed")),
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
Plugin(opts) => opts Plugin(opts) => opts
.plugin() .plugin()
.await .await
.map_err(|e| anyhow!("🚫 Error with plugin: {}", e)), .context(error_wrapper("Error with plugin")),
Autoformat(opts) => opts Autoformat(opts) => opts
.autoformat() .autoformat()
.await .await
.map_err(|e| anyhow!("🚫 Error autoformatting RSX: {}", e)), .context(error_wrapper("Error autoformatting RSX")),
Check(opts) => opts Check(opts) => opts
.check() .check()
.await .await
.map_err(|e| anyhow!("🚫 Error checking RSX: {}", e)), .context(error_wrapper("Error checking RSX")),
Version(opt) => { Version(opt) => {
let version = opt.version(); let version = opt.version();
@@ -85,33 +87,33 @@ async fn main() -> anyhow::Result<()> {
action => { action => {
let bin = get_bin(args.bin)?; let bin = get_bin(args.bin)?;
let _dioxus_config = DioxusConfig::load(Some(bin.clone())) let _dioxus_config = DioxusConfig::load(Some(bin.clone()))
.map_err(|e| anyhow!("Failed to load Dioxus config because: {e}"))? .context("Failed to load Dioxus config because")?
.unwrap_or_else(|| { .unwrap_or_else(|| {
log::info!("You appear to be creating a Dioxus project from scratch; we will use the default config"); log::info!("You appear to be creating a Dioxus project from scratch; we will use the default config");
DioxusConfig::default() DioxusConfig::default()
}); });
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
PluginManager::init(_dioxus_config.plugin) PluginManager::init(_dioxus_config.plugin)
.map_err(|e| anyhow!("🚫 Plugin system initialization failed: {e}"))?; .context(error_wrapper("Plugin system initialization failed"))?;
match action { match action {
Build(opts) => opts Build(opts) => opts
.build(Some(bin.clone()), None) .build(Some(bin.clone()), None)
.map_err(|e| anyhow!("🚫 Building project failed: {}", e)), .context(error_wrapper("Building project failed")),
Clean(opts) => opts Clean(opts) => opts
.clean(Some(bin.clone())) .clean(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Cleaning project failed: {}", e)), .context(error_wrapper("Cleaning project failed")),
Serve(opts) => opts Serve(opts) => opts
.serve(Some(bin.clone())) .serve(Some(bin.clone()))
.await .await
.map_err(|e| anyhow!("🚫 Serving project failed: {}", e)), .context(error_wrapper("Serving project failed")),
Bundle(opts) => opts Bundle(opts) => opts
.bundle(Some(bin.clone())) .bundle(Some(bin.clone()))
.map_err(|e| anyhow!("🚫 Bundling project failed: {}", e)), .context(error_wrapper("Bundling project failed")),
_ => unreachable!(), _ => unreachable!(),
} }