Improved labeled error from plugins (#437)

* improved labeled error from plugins

* corrected span
This commit is contained in:
Fernando Herrera
2021-12-05 03:11:19 +00:00
committed by GitHub
parent 03e22b071a
commit 22469a9cb1
24 changed files with 611 additions and 208 deletions

View File

@ -217,11 +217,17 @@ impl EngineState {
use std::io::Write;
// Updating the signatures plugin file with the added signatures
if let Some(plugin_path) = &self.plugin_signatures {
// Always create the file, which will erase previous signatures
if let Ok(mut plugin_file) = std::fs::File::create(plugin_path.as_path()) {
self.plugin_signatures
.as_ref()
.ok_or_else(|| ShellError::PluginFailedToLoad("Plugin file not found".into()))
.and_then(|plugin_path| {
// Always create the file, which will erase previous signatures
std::fs::File::create(plugin_path.as_path())
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
})
.and_then(|mut plugin_file| {
// Plugin definitions with parsed signature
for decl in self.plugin_decls() {
self.plugin_decls().try_for_each(|decl| {
// A successful plugin registration already includes the plugin filename
// No need to check the None option
let path = decl.is_plugin().expect("plugin should have file name");
@ -234,19 +240,9 @@ impl EngineState {
plugin_file
.write_all(line.as_bytes())
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
})?;
}
Ok(())
} else {
Err(ShellError::PluginFailedToLoad(
"Plugin file not found".into(),
))
}
} else {
Err(ShellError::PluginFailedToLoad(
"Plugin file not found".into(),
))
}
})
})
})
}
pub fn num_files(&self) -> usize {

View File

@ -170,9 +170,17 @@ pub enum ShellError {
FileNotFoundCustom(String, #[label("{0}")] Span),
#[error("Plugin failed to load")]
#[diagnostic(code(nu::shell::plugin_fialed_to_load), url(docsrs))]
#[diagnostic(code(nu::shell::plugin_failed_to_load), url(docsrs))]
PluginFailedToLoad(String),
#[error("Plugin failed to encode")]
#[diagnostic(code(nu::shell::plugin_failed_to_encode), url(docsrs))]
PluginFailedToEncode(String),
#[error("Plugin failed to decode")]
#[diagnostic(code(nu::shell::plugin_failed_to_decode), url(docsrs))]
PluginFailedToDecode(String),
#[error("I/O error")]
#[diagnostic(code(nu::shell::io_error), url(docsrs))]
IOError(String),
@ -224,12 +232,16 @@ pub enum ShellError {
NonUtf8(#[label = "non-UTF8 string"] Span),
#[error("Casting error")]
#[diagnostic(code(nu::parser::downcast_not_possible), url(docsrs))]
#[diagnostic(code(nu::shell::downcast_not_possible), url(docsrs))]
DowncastNotPossible(String, #[label("{0}")] Span),
#[error("{0}")]
#[diagnostic()]
LabeledError(String, String, #[label("{1}")] Span),
SpannedLabeledError(String, String, #[label("{1}")] Span),
#[error("{0}")]
#[diagnostic()]
LabeledError(String, String),
}
impl From<std::io::Error> for ShellError {