Improve the 'use' and 'source' errors (#4966)

* Improve the 'use' and 'source' errors

* Add register
This commit is contained in:
JT 2022-03-26 10:43:46 +13:00 committed by GitHub
parent 19fa41b114
commit 66087b01e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View file

@ -95,7 +95,11 @@ pub enum ParseError {
VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span),
#[error("Module not found.")]
#[diagnostic(code(nu::parser::module_not_found), url(docsrs))]
#[diagnostic(
code(nu::parser::module_not_found),
url(docsrs),
help("module files need to be available before your script is run")
)]
ModuleNotFound(#[label = "module not found"] Span),
#[error("Not found.")]
@ -221,6 +225,22 @@ pub enum ParseError {
#[diagnostic(code(nu::parser::export_not_found), url(docsrs))]
ExportNotFound(#[label = "could not find imports"] Span),
#[error("File not found")]
#[diagnostic(
code(nu::parser::sourced_file_not_found),
url(docsrs),
help("sourced files need to be available before your script is run")
)]
SourcedFileNotFound(String, #[label("File not found: {0}")] Span),
#[error("File not found")]
#[diagnostic(
code(nu::parser::registered_file_not_found),
url(docsrs),
help("registered files need to be available before your script is run")
)]
RegisteredFileNotFound(String, #[label("File not found: {0}")] Span),
#[error("File not found")]
#[diagnostic(code(nu::parser::file_not_found), url(docsrs))]
FileNotFound(String, #[label("File not found: {0}")] Span),
@ -274,6 +294,8 @@ impl ParseError {
ParseError::MissingImportPattern(s) => *s,
ParseError::WrongImportPattern(s) => *s,
ParseError::ExportNotFound(s) => *s,
ParseError::SourcedFileNotFound(_, s) => *s,
ParseError::RegisteredFileNotFound(_, s) => *s,
ParseError::FileNotFound(_, s) => *s,
ParseError::LabeledError(_, _, s) => *s,
}

View file

@ -1853,7 +1853,7 @@ pub fn parse_source(
}
}
} else {
error = error.or(Some(ParseError::FileNotFound(filename, spans[1])));
error = error.or(Some(ParseError::SourcedFileNotFound(filename, spans[1])));
}
} else {
return (garbage_pipeline(spans), Some(ParseError::NonUtf8(spans[1])));
@ -1959,14 +1959,17 @@ pub fn parse_register(
if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_ENV) {
Ok(p)
} else {
Err(ParseError::FileNotFound(name, expr.span))
Err(ParseError::RegisteredFileNotFound(name, expr.span))
}
})
.and_then(|path| {
if path.exists() & path.is_file() {
Ok(path)
} else {
Err(ParseError::FileNotFound(format!("{:?}", path), expr.span))
Err(ParseError::RegisteredFileNotFound(
format!("{:?}", path),
expr.span,
))
}
})
})
@ -2007,13 +2010,17 @@ pub fn parse_register(
String::from_utf8(shell_expr.to_vec())
.map_err(|_| ParseError::NonUtf8(expr.span))
.and_then(|name| {
canonicalize_with(&name, cwd).map_err(|_| ParseError::FileNotFound(name, expr.span))
canonicalize_with(&name, cwd)
.map_err(|_| ParseError::RegisteredFileNotFound(name, expr.span))
})
.and_then(|path| {
if path.exists() & path.is_file() {
Ok(path)
} else {
Err(ParseError::FileNotFound(format!("{:?}", path), expr.span))
Err(ParseError::RegisteredFileNotFound(
format!("{:?}", path),
expr.span,
))
}
})
});