Skip to content

Commit 7741f8e

Browse files
committed
Improve error message for MigrationDirectoryNotFound and ProjectRootNotFound
1 parent 4af0c1c commit 7741f8e

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

diesel/src/migration/errors.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::result;
1313
#[derive(Debug)]
1414
pub enum MigrationError {
1515
/// The migration directory wasn't found
16-
MigrationDirectoryNotFound,
16+
MigrationDirectoryNotFound(PathBuf),
1717
/// Provided migration was in an unknown format
1818
UnknownMigrationFormat(PathBuf),
1919
/// General system IO error
@@ -32,9 +32,10 @@ impl Error for MigrationError {}
3232
impl fmt::Display for MigrationError {
3333
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
3434
match *self {
35-
MigrationError::MigrationDirectoryNotFound => write!(
35+
MigrationError::MigrationDirectoryNotFound(ref p) => write!(
3636
f,
37-
"Unable to find migrations directory in this directory or any parent directories."
37+
"Unable to find migrations directory in {:?} or any parent directories.",
38+
p
3839
),
3940
MigrationError::UnknownMigrationFormat(_) => write!(
4041
f,
@@ -59,8 +60,8 @@ impl PartialEq for MigrationError {
5960
fn eq(&self, other: &Self) -> bool {
6061
match (self, other) {
6162
(
62-
&MigrationError::MigrationDirectoryNotFound,
63-
&MigrationError::MigrationDirectoryNotFound,
63+
&MigrationError::MigrationDirectoryNotFound(_),
64+
&MigrationError::MigrationDirectoryNotFound(_),
6465
) => true,
6566
(
6667
&MigrationError::UnknownMigrationFormat(ref p1),

diesel_cli/src/database_error.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use diesel::result;
22

33
use std::convert::From;
44
use std::error::Error;
5+
use std::path::PathBuf;
56
use std::{fmt, io};
67

78
use self::DatabaseError::*;
@@ -10,7 +11,7 @@ pub type DatabaseResult<T> = Result<T, DatabaseError>;
1011

1112
#[derive(Debug)]
1213
pub enum DatabaseError {
13-
ProjectRootNotFound,
14+
ProjectRootNotFound(PathBuf),
1415
DatabaseUrlMissing,
1516
IoError(io::Error),
1617
QueryError(result::Error),
@@ -40,8 +41,8 @@ impl Error for DatabaseError {}
4041
impl fmt::Display for DatabaseError {
4142
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
4243
match *self {
43-
ProjectRootNotFound => {
44-
f.write_str("Unable to find diesel.toml or Cargo.toml in this directory or any parent directories.")
44+
ProjectRootNotFound(ref p) => {
45+
write!(f, "Unable to find diesel.toml or Cargo.toml in {:?} or any parent directories.", p)
4546
}
4647
DatabaseUrlMissing => {
4748
f.write_str("The --database-url argument must be passed, or the DATABASE_URL environment variable must be set.")
@@ -65,7 +66,7 @@ impl fmt::Display for DatabaseError {
6566
impl PartialEq for DatabaseError {
6667
fn eq(&self, other: &Self) -> bool {
6768
match (self, other) {
68-
(&ProjectRootNotFound, &ProjectRootNotFound) => true,
69+
(&ProjectRootNotFound(_), &ProjectRootNotFound(_)) => true,
6970
_ => false,
7071
}
7172
}

diesel_cli/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ fn search_for_directory_containing_file(path: &Path, file: &str) -> DatabaseResu
316316
} else {
317317
path.parent()
318318
.map(|p| search_for_directory_containing_file(p, file))
319-
.unwrap_or(Err(DatabaseError::ProjectRootNotFound))
319+
.unwrap_or_else(|| Err(DatabaseError::ProjectRootNotFound(path.into())))
320+
.map_err(|_| DatabaseError::ProjectRootNotFound(path.into()))
320321
}
321322
}
322323

diesel_migrations/migrations_internals/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ pub fn search_for_migrations_directory(path: &Path) -> Result<PathBuf, Migration
384384
Ok(migration_path)
385385
} else {
386386
path.parent()
387-
.map(search_for_migrations_directory)
388-
.unwrap_or(Err(MigrationError::MigrationDirectoryNotFound))
387+
.map(|p| search_for_migrations_directory(p))
388+
.unwrap_or_else(|| Err(MigrationError::MigrationDirectoryNotFound(path.into())))
389+
.map_err(|_| MigrationError::MigrationDirectoryNotFound(path.into()))
389390
}
390391
}
391392

0 commit comments

Comments
 (0)