TestAssertionFailure: retain full error chain in message#666
Open
rursprung wants to merge 1 commit intogoogle:mainfrom
Open
TestAssertionFailure: retain full error chain in message#666rursprung wants to merge 1 commit intogoogle:mainfrom
TestAssertionFailure: retain full error chain in message#666rursprung wants to merge 1 commit intogoogle:mainfrom
Conversation
so far the conversion from an `Error` to a `TestAssertionFailure` meant that all information about the source errors were lost. often, the `Display` implementation for `Error` implementations does not include the source information since that should instead be retrieved via `source`. while this has not yet been made into an official API guideline (see [rust-lang/api-guidelines#210]) this is nevertheless being followed. various crates like `anyhow` or `eyere` take care of pretty-printing the error chain on failure, however this does not work with `googletest` since `googletest::Result` has `TestAssertionFailure` as the error type which swallows any `Error` and only keeps the message. to resolve this a simple error chain implementation is added to the `From` implementation which pretty-prints the error chain. example: ``` Error: test3 Caused by: 1: test2 2: test1 ``` fixes google#657 [rust-lang/api-guidelines#210]: rust-lang/api-guidelines#210
gribozavr
requested changes
Aug 20, 2025
| let source2 = CustomError { message: "test2".to_string(), source: Some(source1.into()) }; | ||
| let error = CustomError { message: "test3".to_string(), source: Some(source2.into()) }; | ||
| let assertion_failure = TestAssertionFailure::from(error); | ||
| assert_eq!(assertion_failure.description, "Error: test3\n\nCaused by:\n1: test2\n2: test1"); |
Collaborator
There was a problem hiding this comment.
Suggested change
| assert_eq!(assertion_failure.description, "Error: test3\n\nCaused by:\n1: test2\n2: test1"); | |
| verify_eq!(assertion_failure.description, "Error: test3\n\nCaused by:\n1: test2\n2: test1") |
... and change the return type to Result<()> (see comment above)
| use std::fmt::{Debug, Display, Formatter}; | ||
|
|
||
| #[test] | ||
| fn error_chain_from_error() { |
Collaborator
There was a problem hiding this comment.
Suggested change
| fn error_chain_from_error() { | |
| fn test_assertion_failure_chain_from_error() -> Result<()>{ |
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { |
Collaborator
There was a problem hiding this comment.
Could you do a bit more testing:
-
Check whether we can / should update the error message verification in
test_can_return_anyhow_generated_error()in integration_tests/src/integration_tests.rs -
Add a new integration test that returns a Result with a custom error type like integration_tests/src/test_returning_anyhow_error.rs
Collaborator
|
Thank you for the fix! This PR generally looks good, only a few stylistic comments and a request to add an integration test. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
so far the conversion from an
Errorto aTestAssertionFailuremeant that all information about the source errors were lost. often, theDisplayimplementation forErrorimplementations does not include the source information since that should instead be retrieved viasource. while this has not yet been made into an official API guideline (see rust-lang/api-guidelines#210) this is nevertheless being followed.various crates like
anyhoworeyeretake care of pretty-printing the error chain on failure, however this does not work withgoogletestsincegoogletest::ResulthasTestAssertionFailureas the error type which swallows anyErrorand only keeps the message.to resolve this a simple error chain implementation is added to the
Fromimplementation which pretty-prints the error chain.example:
fixes #657