update miette and switch to GenericErrors (#5222)

This commit is contained in:
Kat Marchán
2022-04-18 05:34:10 -07:00
committed by GitHub
parent cf65f77b02
commit 1314a87cb0
141 changed files with 1569 additions and 689 deletions

View File

@ -46,9 +46,9 @@ impl Command for Cd {
let path = match nu_path::canonicalize_with(path, &cwd) {
Ok(p) => p,
Err(e) => {
return Err(ShellError::DirectoryNotFoundHelp(
return Err(ShellError::DirectoryNotFound(
*span,
format!("IO Error: {:?}", e),
Some(format!("IO Error: {:?}", e)),
))
}
};
@ -69,9 +69,9 @@ impl Command for Cd {
}
Err(e) => {
return Err(ShellError::DirectoryNotFoundHelp(
return Err(ShellError::DirectoryNotFound(
v.span()?,
format!("IO Error: {:?}", e),
Some(format!("IO Error: {:?}", e)),
))
}
};

View File

@ -61,37 +61,45 @@ impl Command for Cp {
let sources: Vec<_> = match nu_glob::glob_with(&source.to_string_lossy(), GLOB_PARAMS) {
Ok(files) => files.collect(),
Err(e) => {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
e.to_string(),
"invalid pattern".to_string(),
src.span,
Some(src.span),
None,
Vec::new(),
))
}
};
if sources.is_empty() {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"No matches found".into(),
"no matches found".into(),
src.span,
Some(src.span),
None,
Vec::new(),
));
}
if sources.len() > 1 && !destination.is_dir() {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Destination must be a directory when copying multiple files".into(),
"is not a directory".into(),
dst.span,
Some(dst.span),
None,
Vec::new(),
));
}
let any_source_is_dir = sources.iter().any(|f| matches!(f, Ok(f) if f.is_dir()));
if any_source_is_dir && !recursive {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Directories must be copied using \"--recursive\"".into(),
"resolves to a directory (not copied)".into(),
src.span,
Some(src.span),
None,
Vec::new(),
));
}
@ -115,7 +123,13 @@ impl Command for Cp {
for (src, dst) in sources {
if src.is_file() {
std::fs::copy(src, dst).map_err(|e| {
ShellError::SpannedLabeledError(e.to_string(), e.to_string(), call.head)
ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(call.head),
None,
Vec::new(),
)
})?;
}
}
@ -126,17 +140,25 @@ impl Command for Cp {
match entry.file_name() {
Some(name) => destination.join(name),
None => {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Copy aborted. Not a valid path".into(),
"not a valid path".into(),
dst.span,
Some(dst.span),
None,
Vec::new(),
))
}
}
};
std::fs::create_dir_all(&destination).map_err(|e| {
ShellError::SpannedLabeledError(e.to_string(), e.to_string(), dst.span)
ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(dst.span),
None,
Vec::new(),
)
})?;
let sources = sources.paths_applying_with(|(source_file, depth_level)| {
@ -161,13 +183,25 @@ impl Command for Cp {
for (s, d) in sources {
if s.is_dir() && !d.exists() {
std::fs::create_dir_all(&d).map_err(|e| {
ShellError::SpannedLabeledError(e.to_string(), e.to_string(), dst.span)
ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(dst.span),
None,
Vec::new(),
)
})?;
}
if s.is_file() {
std::fs::copy(&s, &d).map_err(|e| {
ShellError::SpannedLabeledError(e.to_string(), e.to_string(), call.head)
ShellError::GenericError(
e.to_string(),
e.to_string(),
Some(call.head),
None,
Vec::new(),
)
})?;
}
}

View File

@ -104,9 +104,12 @@ impl Command for Glob {
let glob = match WaxGlob::new(&glob_pattern.item) {
Ok(p) => p,
Err(e) => {
return Err(ShellError::LabeledError(
return Err(ShellError::GenericError(
"error with glob pattern".to_string(),
format!("{}", e),
"".to_string(),
None,
Some(format!("{}", e)),
Vec::new(),
))
}
};

View File

@ -96,10 +96,12 @@ impl Command for Ls {
);
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Permission denied".to_string(),
error_msg,
p_tag,
Some(p_tag),
None,
Vec::new(),
));
}
if is_empty_dir(&expanded) {
@ -129,9 +131,12 @@ impl Command for Ls {
let mut paths_peek = paths.peekable();
if paths_peek.peek().is_none() {
return Err(ShellError::LabeledError(
return Err(ShellError::GenericError(
format!("No matches found for {}", &path.display().to_string()),
"no matches found".to_string(),
"".to_string(),
None,
Some("no matches found".to_string()),
Vec::new(),
));
}
@ -176,10 +181,12 @@ impl Command for Ls {
Some(path.to_string_lossy().to_string())
}
.ok_or_else(|| {
ShellError::SpannedLabeledError(
ShellError::GenericError(
format!("Invalid file name: {:}", path.to_string_lossy()),
"invalid file name".into(),
call_span,
Some(call_span),
None,
Vec::new(),
)
});

View File

@ -71,10 +71,12 @@ impl Command for Mv {
.map_or_else(|_| Vec::new(), Iterator::collect);
if sources.is_empty() {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Invalid file or pattern".into(),
"invalid file or pattern".into(),
spanned_source.span,
Some(spanned_source.span),
None,
Vec::new(),
));
}
@ -91,10 +93,12 @@ impl Command for Mv {
if (destination.exists() && !destination.is_dir() && sources.len() > 1)
|| (!destination.exists() && sources.len() > 1)
{
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Can only move multiple sources if destination is a directory".into(),
"destination must be a directory when multiple sources".into(),
spanned_destination.span,
Some(spanned_destination.span),
None,
Vec::new(),
));
}
@ -103,13 +107,15 @@ impl Command for Mv {
.find(|f| matches!(f, Ok(f) if destination.starts_with(f)));
if destination.exists() && destination.is_dir() && sources.len() == 1 {
if let Some(Ok(filename)) = some_if_source_is_destination {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
format!(
"Not possible to move {:?} to itself",
filename.file_name().expect("Invalid file name")
),
"cannot move to itself".into(),
spanned_destination.span,
Some(spanned_destination.span),
None,
Vec::new(),
));
}
}
@ -202,14 +208,14 @@ fn move_file(
};
if !destination_dir_exists {
return Err(ShellError::DirectoryNotFound(to_span));
return Err(ShellError::DirectoryNotFound(to_span, None));
}
let mut to = to;
if to.is_dir() {
let from_file_name = match from.file_name() {
Some(name) => name,
None => return Err(ShellError::DirectoryNotFound(to_span)),
None => return Err(ShellError::DirectoryNotFound(to_span, None)),
};
to.push(from_file_name);
@ -233,10 +239,12 @@ fn move_item(from: &Path, from_span: Span, to: &Path) -> Result<(), ShellError>
fs_extra::dir::move_dir(from, to, &options)
} {
Ok(_) => Ok(()),
Err(e) => Err(ShellError::SpannedLabeledError(
Err(e) => Err(ShellError::GenericError(
format!("Could not move {:?} to {:?}. {:}", from, to, e),
"could not move".into(),
from_span,
Some(from_span),
None,
Vec::new(),
)),
}
})

View File

@ -94,19 +94,23 @@ impl Command for Open {
);
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
Err(ShellError::SpannedLabeledError(
Err(ShellError::GenericError(
"Permission denied".into(),
error_msg,
arg_span,
Some(arg_span),
None,
Vec::new(),
))
} else {
let mut file = match std::fs::File::open(path) {
Ok(file) => file,
Err(err) => {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Permission denied".into(),
err.to_string(),
arg_span,
Some(arg_span),
None,
Vec::new(),
));
}
};
@ -198,16 +202,20 @@ fn open_and_read_sqlite_db(path: &Path, call_span: Span) -> Result<Value, nu_pro
match Connection::open(path) {
Ok(conn) => match read_sqlite_db(conn, call_span) {
Ok(data) => Ok(data),
Err(err) => Err(ShellError::SpannedLabeledError(
Err(err) => Err(ShellError::GenericError(
"Failed to read from SQLite database".into(),
err.to_string(),
call_span,
Some(call_span),
None,
Vec::new(),
)),
},
Err(err) => Err(ShellError::SpannedLabeledError(
Err(err) => Err(ShellError::GenericError(
"Failed to open SQLite database".into(),
err.to_string(),
call_span,
Some(call_span),
None,
Vec::new(),
)),
}
}

View File

@ -144,32 +144,38 @@ fn rm(
))]
{
if rm_always_trash {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Cannot execute `rm`; the current configuration specifies \
`rm_always_trash = true`, but the current nu executable was not \
built with feature `trash_support` or trash is not supported on \
your platform."
.into(),
"trash required to be true but not supported".into(),
span,
Some(span),
None,
Vec::new(),
));
} else if trash {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Cannot execute `rm` with option `--trash`; feature `trash-support` not \
enabled or trash is not supported on your platform"
.into(),
"this option is only available if nu is built with the `trash-support` feature"
.into(),
span,
Some(span),
None,
Vec::new(),
));
}
}
if targets.is_empty() {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"rm requires target paths".into(),
"needs parameter".into(),
span,
Some(span),
None,
Vec::new(),
));
}
@ -184,10 +190,12 @@ fn rm(
std::path::MAIN_SEPARATOR
))
{
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Cannot remove any parent directory".into(),
"cannot remove any parent directory".into(),
target.span,
Some(target.span),
None,
Vec::new(),
));
}
@ -214,30 +222,36 @@ fn rm(
all_targets.entry(f.clone()).or_insert_with(|| target.span);
}
Err(e) => {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
format!("Could not remove {:}", path.to_string_lossy()),
e.to_string(),
target.span,
Some(target.span),
None,
Vec::new(),
));
}
}
}
}
Err(e) => {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
e.to_string(),
e.to_string(),
call.head,
Some(call.head),
None,
Vec::new(),
))
}
};
}
if all_targets.is_empty() && !force {
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"No valid paths".into(),
"no valid paths".into(),
call.head,
Some(call.head),
None,
Vec::new(),
));
}
@ -301,7 +315,13 @@ fn rm(
if let Err(e) = result {
let msg = format!("Could not delete because: {:}\nTry '--trash' flag", e);
Value::Error {
error: ShellError::SpannedLabeledError(msg, e.to_string(), span),
error: ShellError::GenericError(
msg,
e.to_string(),
Some(span),
None,
Vec::new(),
),
}
} else if quiet {
Value::Nothing { span }
@ -312,20 +332,24 @@ fn rm(
} else {
let msg = format!("Cannot remove {:}. try --recursive", f.to_string_lossy());
Value::Error {
error: ShellError::SpannedLabeledError(
error: ShellError::GenericError(
msg,
"cannot remove non-empty directory".into(),
span,
Some(span),
None,
Vec::new(),
),
}
}
} else {
let msg = format!("no such file or directory: {:}", f.to_string_lossy());
Value::Error {
error: ShellError::SpannedLabeledError(
error: ShellError::GenericError(
msg,
"no such file or directory".into(),
span,
Some(span),
None,
Vec::new(),
),
}
}

View File

@ -57,10 +57,12 @@ impl Command for Save {
Err(err) => {
return Ok(PipelineData::Value(
Value::Error {
error: ShellError::SpannedLabeledError(
error: ShellError::GenericError(
"Permission denied".into(),
err.to_string(),
arg_span,
Some(arg_span),
None,
Vec::new(),
),
},
None,