Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#5235·diesel

Error wrappers do not expose consistent source chains

Author: Turbo87Created Sep 22, 2026Updated Sep 22, 2026
Labelsbughelp wanted

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:

  1. Some wrappers render the underlying error through Display, but expose it only through the deprecated Error::cause() or do not expose it at all. Modern chain-aware reporters therefore cannot traverse the complete error chain.

  2. Some wrappers render the underlying error through Display and also return it from source(). Chain-aware reporters can then print the same underlying message twice.

A preliminary audit found the following affected types:

  • diesel::result::Error
    • InvalidCString
    • QueryBuilderError
    • DeserializationError
    • SerializationError
    • IntegerConversion
  • diesel::result::ConnectionError
    • InvalidCString
    • CouldntSetupConfiguration
  • diesel::result::DeserializeFieldError
  • diesel::r2d2::Error
    • ConnectionError
    • QueryError
  • diesel_migrations::MigrationError::IoError
  • diesel_migrations::RunMigrationsError
    • MigrationError
    • QueryError
  • diesel_infer_query::Error
    • ParserError exposes and renders its source
    • ResolverFailure renders but does not expose its inner error
  • The internal diesel_cli::errors::Error enum 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:

  • Display describes 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 value

Additional 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:

rust
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
None

The 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

View original on GitHubView discussion on GitHub