Skip to content

Commit

Permalink
Merge pull request #8 from wiktor-k/improve-details
Browse files Browse the repository at this point in the history
Improve printing error details when using anyhow
  • Loading branch information
wiktor-k authored Mar 12, 2024
2 parents 151f9c3 + ac18dc9 commit 0883161
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: CI

on: [push, pull_request]
on:
pull_request:
push:
tags:
- 'v*'
branches: [ main ]
workflow_dispatch:

jobs:
# Use the following command to fix words locally:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ exclude = [".github"]
[dependencies]

[dev-dependencies]
anyhow = "1.0.81"
rstest = "0.18.2"
53 changes: 49 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub enum TestError {}
impl<T: std::fmt::Display> From<T> for TestError {
#[track_caller] // Will show the location of the caller in test failure messages
fn from(error: T) -> Self {
panic!("error: {} - {}", std::any::type_name::<T>(), error);
// Use alternate format for rich error message for anyhow
// See: https://docs.rs/anyhow/latest/anyhow/struct.Error.html#display-representations
panic!("error: {} - {:#}", std::any::type_name::<T>(), error);
}
}

Expand Down Expand Up @@ -71,6 +73,7 @@ pub type TestResult<T = ()> = std::result::Result<T, TestError>;
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Context as _;

#[test]
#[ignore] // ignored test must still compile
Expand All @@ -81,9 +84,9 @@ mod tests {
}

// helper function which always fails
fn test_fn() -> TestResult<std::fs::File> {
let file = std::fs::File::open("this-file-does-not-exist")?;
Ok(file)
fn test_fn() -> TestResult<String> {
let string = String::from_utf8(vec![0, 159, 146, 150])?;
Ok(string)
}

#[test]
Expand All @@ -92,6 +95,48 @@ mod tests {
let _ = test_fn();
});
assert!(result.is_err());

let err = result.unwrap_err();
assert_eq!(
Some(
&"error: alloc::string::FromUtf8Error - invalid utf-8 sequence of 1 bytes from index 1"
.to_string()
),
err.downcast_ref::<String>()
);
Ok(())
}

fn anyhow_a() -> anyhow::Result<String> {
let string = String::from_utf8(vec![0, 159, 146, 150])?;
Ok(string)
}

fn anyhow_b() -> anyhow::Result<String> {
let file = anyhow_a().context("Parsing a string")?;
Ok(file)
}

fn anyhow_c() -> TestResult<String> {
let file = anyhow_b()?;
Ok(file)
}

#[test]
fn check_if_anyhow_panics() -> TestResult {
let result = std::panic::catch_unwind(|| {
let _ = anyhow_c();
});
assert!(result.is_err());

let err = result.unwrap_err();
assert_eq!(
Some(
&"error: anyhow::Error - Parsing a string: invalid utf-8 sequence of 1 bytes from index 1"
.to_string()
),
err.downcast_ref::<String>()
);
Ok(())
}
}

0 comments on commit 0883161

Please sign in to comment.