Skip to content

Commit

Permalink
chore: improved error messages
Browse files Browse the repository at this point in the history
Removed `Debug::fmt` usage.
  • Loading branch information
Andrew15-5 committed Dec 18, 2024
1 parent 013069d commit 7ecb23c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,9 @@ fn retrieve_assets<'a>(
let style = match (style, csl) {
(_, Some(csl)) => {
let xml_str = fs::read_to_string(csl).ok().ok_or(OtherError(format!(
"could not read CSL file: {csl:?}\n{}",
"Maybe you meant to use --style instead?"
r#"Could not read CSL file: "{}"{}"#,
csl.display(),
"\nMaybe you meant to use --style instead?"
)))?;
IndependentStyle::from_xml(&xml_str)
.map_err(|_| OtherError("CSL file malformed".into()))?
Expand Down
34 changes: 17 additions & 17 deletions src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use std::path::PathBuf;
#[cfg(feature = "biblatex")]
pub use biblatex_module::*;

/// Used once before all but the 1st errors.
const TOP_INDENT: &str = "\nCaused by:";

/// Used to indent next error.
const INDENT: &str = " ";

/// The main error that is handled in the `main()` function.
#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -11,10 +17,10 @@ pub enum Error {
BibliographyError(#[from] BibliographyError),
/// Invalid Bib(La)TeX input file format.
#[cfg(feature = "biblatex")]
#[error("Invalid format: expected Bib(La)TeX\nCaused by:\n\t{0:?}")]
#[error("Invalid format: expected Bib(La)TeX\n{TOP_INDENT}\n{INDENT}{0}")]
InvalidBiblatex(#[from] BibLaTeXErrors),
/// Invalid YAML input file format.
#[error("Invalid format: expected YAML\nCaused by:\n\t{0:?}")]
#[error("Invalid format: expected YAML\n{TOP_INDENT}\n{INDENT}{0}")]
InvalidYaml(#[from] serde_yaml::Error),
/// Other error.
#[error("{0}")]
Expand All @@ -25,13 +31,13 @@ pub enum Error {
#[derive(thiserror::Error, Debug)]
pub enum BibliographyError {
/// Bibliography file not found.
#[error("Bibliography file {0:?} not found.")]
#[error(r#"Bibliography file "{0}" not found."#)]
NotFound(PathBuf),
/// Error while reading the bibliography file (with OS error code).
#[error("Error while reading the bibliography file {0:?}: {1}")]
#[error(r#"Error while reading the bibliography file "{0}": {1}"#)]
ReadErrorWithCode(PathBuf, i32),
/// Error while reading the bibliography file.
#[error("Error while reading the bibliography file {0:?}.")]
#[error(r#"Error while reading the bibliography file "{0}"."#)]
ReadError(PathBuf),
}

Expand All @@ -43,37 +49,31 @@ impl From<&str> for Error {

#[cfg(feature = "biblatex")]
mod biblatex_module {
use std::{error::Error, fmt::Display};
use super::INDENT;
use std::fmt::{Display, Formatter, Result};

/// Errors that may occur when parsing a BibLaTeX file.
#[derive(thiserror::Error, Clone, Debug)]
pub enum BibLaTeXError {
/// An error occurred when parsing a BibLaTeX file.
#[error("BibLaTeX parse error\n{INDENT}{0}")]
Parse(biblatex::ParseError),
/// One of the BibLaTeX fields was malformed for its type.
#[error("BibLaTeX type error\n{INDENT}{0}")]
Type(biblatex::TypeError),
}

impl Display for BibLaTeXError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Parse(err) => write!(f, "biblatex parse error: {}", err),
Self::Type(err) => write!(f, "biblatex type error: {}", err),
}
}
}

/// Wrapper over an array of `BibLaTeXError` elements.
#[derive(thiserror::Error, Clone, Debug)]
pub struct BibLaTeXErrors(pub Vec<BibLaTeXError>);

impl Display for BibLaTeXErrors {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let sources = self
.0
.clone()
.into_iter()
.filter_map(|x| x.source().map(|err| format!("{err:?}")))
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join("\n\t");
write!(f, "{sources}")
Expand Down

0 comments on commit 7ecb23c

Please sign in to comment.