Skip to content

Commit

Permalink
error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskrycho committed Nov 8, 2024
1 parent f4e1321 commit 110fbee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/mdbook_trpl/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Get any `preprocessor.trpl` config.
//! Get any `preprocessor.trpl-*` config.

use mdbook::preprocess::PreprocessorContext;

Expand Down Expand Up @@ -45,7 +45,7 @@ pub struct ParseErr;
impl TryFrom<&str> for Mode {
type Error = ParseErr;

fn try_from(value: &str) -> std::prelude::v1::Result<Self, Self::Error> {
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"default" => Ok(Mode::Default),
"simple" => Ok(Mode::Simple),
Expand All @@ -57,7 +57,7 @@ impl TryFrom<&str> for Mode {
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Mdbook(mdbook::errors::Error),
Mdbook(#[from] mdbook::errors::Error),

#[error("No config for '{0}'")]
NoConfig(String),
Expand Down
32 changes: 32 additions & 0 deletions packages/mdbook_trpl/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub struct Report(Box<dyn std::error::Error>);

#[derive(Debug, thiserror::Error)]
#[error("{0}")]
struct Simple(String);

impl Report {
pub fn simple(s: impl Into<String>) -> Report {
Report(Box::new(Simple(s.into())))
}
}

impl<E> From<E> for Report
where
E: std::error::Error + 'static,
{
fn from(value: E) -> Self {
Report(Box::new(value))
}
}

impl std::fmt::Debug for Report {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{:?}", self.0)?;
let mut current_err = self.0.as_ref();
while let Some(source_err) = current_err.source() {
writeln!(f, "Caused by: {:?}", source_err)?;
current_err = source_err;
}
Ok(())
}
}

0 comments on commit 110fbee

Please sign in to comment.