Skip to content

Commit 6848ef3

Browse files
authored
[cli] return, not exit, from get_migrations (#335)
Previously, the cli-internal method `get_migrations` would print an error and call `std::process::exit` if no migrations directory was found, despite the fact that it has a `Result` return type. This caused unintended consequences in #100, which unconditionally calls `get_migrations` on process start. It is expected that no migrations directory would exist prior to invoking `butane init`, but this invocation causes `butane init` to fail. It's both less-surprising and more idiomatic rust for `get_migrations` to return an error rather than failing. Fixes #331, however this issue also highlights that we have a large test gap with butane-cli. This change does not close that gap.
1 parent 1336605 commit 6848ef3

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ serde_json = "1.0"
5353
sqlparser = "0.55"
5454
syn = { version = "2", features = ["extra-traits", "full"] }
5555
tempfile = "3.10"
56+
thiserror = "2.0"
5657
tokio = { version = "1"}
5758
tokio-postgres = "0.7"
5859
tokio-test = { version = "0.4"}

butane_cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ nonempty.workspace = true
3636
quote = { workspace = true }
3737
serde = { workspace = true }
3838
serde_json = { workspace = true }
39+
thiserror = { workspace = true }

butane_cli/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,11 @@ pub fn clean(base_dir: &Path) -> Result<()> {
666666

667667
pub fn get_migrations(base_dir: &Path) -> Result<FsMigrations> {
668668
let root = base_dir.join("migrations");
669-
if !root.is_dir() {
670-
eprintln!("No butane migrations directory found. Add at least one model to your project and build.");
671-
std::process::exit(1);
669+
if root.is_dir() {
670+
Ok(migrations::from_root(root))
671+
} else {
672+
Err(anyhow::Error::new(CliError::NoButaneMigrationsDir))
672673
}
673-
Ok(migrations::from_root(root))
674674
}
675675

676676
pub fn working_dir_path() -> PathBuf {
@@ -739,9 +739,20 @@ pub fn base_dir() -> PathBuf {
739739
current_directory
740740
}
741741

742+
#[derive(thiserror::Error, Debug)]
743+
pub enum CliError {
744+
#[error(
745+
"No butane migrations directory found. Add at least one model to your project and build."
746+
)]
747+
NoButaneMigrationsDir,
748+
}
749+
742750
pub fn handle_error(r: Result<()>) {
743751
if let Err(e) = r {
744-
eprintln!("Encountered unexpected error: {e}");
752+
match e.downcast_ref::<CliError>() {
753+
Some(e2) => eprintln!("{e2}"),
754+
None => eprintln!("Encountered unexpected error: {e}"),
755+
}
745756
std::process::exit(1);
746757
}
747758
}

butane_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ serde = { features = ["derive"], workspace = true }
5454
serde_json = { workspace = true }
5555
sqlparser = { workspace = true }
5656
syn = { workspace = true }
57-
thiserror = "2.0"
57+
thiserror = { workspace = true }
5858
uuid = { workspace = true, optional = true }
5959

6060
[dev-dependencies]

0 commit comments

Comments
 (0)