summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-08-13 19:03:37 +0300
committerLars Wirzenius <liw@liw.fi>2022-08-13 19:03:37 +0300
commit9b3b0087512614d13e8c3b950041101ab649b786 (patch)
tree8dbf69f30988038b9907cd2462790ee2d3790fb3 /src
parent60b5a9a313bec0a6e475f9c16380977ed1c7eafb (diff)
downloadsubplot-9b3b0087512614d13e8c3b950041101ab649b786.tar.gz
feat: make errors have a source error
When errors have a source, i.e., an underlying error, it results in better error messages to the user. Also, refactor the way the source chain is printed, to code that is clearer to me. I will need this to for loading document metadata from an external YAML file. Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r--src/bin/subplot.rs13
-rw-r--r--src/error.rs18
2 files changed, 16 insertions, 15 deletions
diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs
index e9cb00a..5e802b4 100644
--- a/src/bin/subplot.rs
+++ b/src/bin/subplot.rs
@@ -504,13 +504,6 @@ fn load_linted_doc(
Ok(doc)
}
-fn print_source_errors(e: Option<&dyn std::error::Error>) {
- if let Some(e) = e {
- error!("{}", e);
- print_source_errors(e.source());
- }
-}
-
fn real_main() {
info!("Starting Subplot");
let argparser = Toplevel::command();
@@ -525,7 +518,11 @@ fn real_main() {
}
Err(e) => {
error!("{}", e);
- print_source_errors(e.source());
+ let mut e = e.source();
+ while let Some(source) = e {
+ error!("caused by: {}", source);
+ e = source.source();
+ }
process::exit(1);
}
}
diff --git a/src/error.rs b/src/error.rs
index a42298d..d714d21 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -13,12 +13,12 @@ pub enum SubplotError {
Warnings(usize),
/// Subplot could not find a file named as a bindings file.
- #[error("binding file could not be found: {0}: {1}")]
- BindingsFileNotFound(PathBuf, std::io::Error),
+ #[error("binding file could not be found: {0}")]
+ BindingsFileNotFound(PathBuf, #[source] std::io::Error),
/// Subplot could not find a file named as a functions file.
- #[error("functions file could not be found: {0}: {1}")]
- FunctionsFileNotFound(PathBuf, std::io::Error),
+ #[error("functions file could not be found: {0}")]
+ FunctionsFileNotFound(PathBuf, #[source] std::io::Error),
/// The simple pattern specifies a kind that is unknown.
#[error("simple pattern kind {0} is unknown")]
@@ -201,7 +201,7 @@ pub enum SubplotError {
///
/// The `add-newline` attribute can only take the values `auto`, `yes`,
/// and `no`.
- #[error("Embedded file {0} has unrecognised `add-newline={}` - valid values are auto/yes/no")]
+ #[error("Embedded file {0} has unrecognised `add-newline={1}` - valid values are auto/yes/no")]
UnrecognisedAddNewline(String, String),
/// Couldn't determine base directory from input file name.
@@ -233,8 +233,8 @@ pub enum SubplotError {
NoTemplateSpecDirectory(PathBuf),
/// A code template has an error.
- #[error("Couldn't load template {0}: {1}")]
- TemplateError(String, tera::Error),
+ #[error("Couldn't load template {0}")]
+ TemplateError(String, #[source] tera::Error),
/// Unknown classes in use in document
#[error("Unknown classes found in the document: {0}")]
@@ -302,6 +302,10 @@ pub enum SubplotError {
#[error("Failed to parse YAML metadata")]
Metadata(#[source] serde_yaml::Error),
+ /// Error parsing YAML metadata for document, from external file.
+ #[error("Failed to parse YAML metadata in {0}")]
+ MetadataFile(PathBuf, #[source] serde_yaml::Error),
+
/// Abstract syntax tree error.
#[error(transparent)]
Ast(#[from] crate::ast::Error),