Skip to content

Commit 7ecb23c

Browse files
committed
chore: improved error messages
Removed `Debug::fmt` usage.
1 parent 013069d commit 7ecb23c

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,9 @@ fn retrieve_assets<'a>(
457457
let style = match (style, csl) {
458458
(_, Some(csl)) => {
459459
let xml_str = fs::read_to_string(csl).ok().ok_or(OtherError(format!(
460-
"could not read CSL file: {csl:?}\n{}",
461-
"Maybe you meant to use --style instead?"
460+
r#"Could not read CSL file: "{}"{}"#,
461+
csl.display(),
462+
"\nMaybe you meant to use --style instead?"
462463
)))?;
463464
IndependentStyle::from_xml(&xml_str)
464465
.map_err(|_| OtherError("CSL file malformed".into()))?

src/types/error.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ use std::path::PathBuf;
33
#[cfg(feature = "biblatex")]
44
pub use biblatex_module::*;
55

6+
/// Used once before all but the 1st errors.
7+
const TOP_INDENT: &str = "\nCaused by:";
8+
9+
/// Used to indent next error.
10+
const INDENT: &str = " ";
11+
612
/// The main error that is handled in the `main()` function.
713
#[derive(thiserror::Error, Debug)]
814
pub enum Error {
@@ -11,10 +17,10 @@ pub enum Error {
1117
BibliographyError(#[from] BibliographyError),
1218
/// Invalid Bib(La)TeX input file format.
1319
#[cfg(feature = "biblatex")]
14-
#[error("Invalid format: expected Bib(La)TeX\nCaused by:\n\t{0:?}")]
20+
#[error("Invalid format: expected Bib(La)TeX\n{TOP_INDENT}\n{INDENT}{0}")]
1521
InvalidBiblatex(#[from] BibLaTeXErrors),
1622
/// Invalid YAML input file format.
17-
#[error("Invalid format: expected YAML\nCaused by:\n\t{0:?}")]
23+
#[error("Invalid format: expected YAML\n{TOP_INDENT}\n{INDENT}{0}")]
1824
InvalidYaml(#[from] serde_yaml::Error),
1925
/// Other error.
2026
#[error("{0}")]
@@ -25,13 +31,13 @@ pub enum Error {
2531
#[derive(thiserror::Error, Debug)]
2632
pub enum BibliographyError {
2733
/// Bibliography file not found.
28-
#[error("Bibliography file {0:?} not found.")]
34+
#[error(r#"Bibliography file "{0}" not found."#)]
2935
NotFound(PathBuf),
3036
/// Error while reading the bibliography file (with OS error code).
31-
#[error("Error while reading the bibliography file {0:?}: {1}")]
37+
#[error(r#"Error while reading the bibliography file "{0}": {1}"#)]
3238
ReadErrorWithCode(PathBuf, i32),
3339
/// Error while reading the bibliography file.
34-
#[error("Error while reading the bibliography file {0:?}.")]
40+
#[error(r#"Error while reading the bibliography file "{0}"."#)]
3541
ReadError(PathBuf),
3642
}
3743

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

4450
#[cfg(feature = "biblatex")]
4551
mod biblatex_module {
46-
use std::{error::Error, fmt::Display};
52+
use super::INDENT;
53+
use std::fmt::{Display, Formatter, Result};
4754

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

57-
impl Display for BibLaTeXError {
58-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59-
match self {
60-
Self::Parse(err) => write!(f, "biblatex parse error: {}", err),
61-
Self::Type(err) => write!(f, "biblatex type error: {}", err),
62-
}
63-
}
64-
}
65-
6666
/// Wrapper over an array of `BibLaTeXError` elements.
6767
#[derive(thiserror::Error, Clone, Debug)]
6868
pub struct BibLaTeXErrors(pub Vec<BibLaTeXError>);
6969

7070
impl Display for BibLaTeXErrors {
71-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
7272
let sources = self
7373
.0
7474
.clone()
7575
.into_iter()
76-
.filter_map(|x| x.source().map(|err| format!("{err:?}")))
76+
.map(|x| x.to_string())
7777
.collect::<Vec<String>>()
7878
.join("\n\t");
7979
write!(f, "{sources}")

0 commit comments

Comments
 (0)