Error wrappers do not expose consistent source chains
Rust Version
1.90.0
Diesel Version
Current main (85c79215, version 2.3.13)
Diesel Features
Not feature-specific. Additional affected types exist behind features such as r2d2.
Database Version
Not database-specific
Operating System Version
Not OS-specific
What happened?
Several error types across the Diesel workspace wrap underlying errors but do not represent those errors consistently through std::error::Error::source().
There are two related patterns:
Some wrappers render the underlying error through
Display, but expose it only through the deprecatedError::cause()or do not expose it at all. Modern chain-aware reporters therefore cannot traverse the complete error chain.Some wrappers render the underlying error through
Displayand also return it fromsource(). Chain-aware reporters can then print the same underlying message twice.
A preliminary audit found the following affected types:
diesel::result::ErrorInvalidCStringQueryBuilderErrorDeserializationErrorSerializationErrorIntegerConversion
diesel::result::ConnectionErrorInvalidCStringCouldntSetupConfiguration
diesel::result::DeserializeFieldErrordiesel::r2d2::ErrorConnectionErrorQueryError
diesel_migrations::MigrationError::IoErrordiesel_migrations::RunMigrationsErrorMigrationErrorQueryError
diesel_infer_query::ErrorParserErrorexposes and renders its sourceResolverFailurerenders but does not expose its inner error
- The internal
diesel_cli::errors::Errorenum has several variants with the same inconsistency. Variants using#[from]or#[source]render and expose their source, while other wrapper variants render an inner error without exposing it.
diesel::result::Error and ConnectionError currently implement the deprecated cause() method rather than source(). Because the default source() implementation returns None, modern error reporters do not see those causes. The default impl for cause() uses source() though.
The deserialization path demonstrates both sides of the problem. Error::DeserializationError delegates its display output to the wrapped error but returns None from source(). The wrapped DeserializeFieldError, however, includes its decoding error in Display and also returns that error from source().
What did you expect to happen?
Diesel error wrappers should follow the conventions from the std lib docs:
Displaydescribes the context added by the current error layer.source()returns the underlying error, where one exists.- A chain-aware reporter renders each message exactly once.
For example, a deserialization report could produce:
Failed to deserialize query result
caused by: Error deserializing field 'name'
caused by: invalid valueAdditional details
The standard library documentation recommends that an error wrapper either include the underlying error in its Display output or expose it through source(), but not both:
https://doc.rust-lang.org/std/error/trait.Error.html#error-source
Would you be open to making error-source handling consistent across the workspace? I would be happy to prepare the changes with a bit of guidance.
Steps to reproduce
The deserialization path provides a small example:
use std::error::Error as _;
use std::io;
use diesel::result::Error;
fn main() {
let error =
Error::DeserializationError(Box::new(io::Error::other("invalid value")));
println!("{error}");
println!("{:?}", error.source().map(ToString::to_string));
}This prints:
invalid value
NoneThe wrapped error supplies the outer error’s entire display text, but modern source-chain traversal cannot reach it.
DeserializeFieldError demonstrates the inverse behavior: its display output includes the underlying decoding error even though its source() method returns that same error.
Compile time error
No response
Checklist
- I have already looked over the issue tracker and the discussion forum for similar possible closed issues.
- This issue can be reproduced on Rust's stable channel. (Please submit the issue in the Rust issue tracker instead if that's not the case)
- This issue can be reproduced without requiring a third party crate
Source: diesel-rs/diesel