Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GEN-152: report::from should load recursive .source() information from std::error::Error-compatible errors #4355

Closed
QAston opened this issue Apr 25, 2024 · 2 comments
Assignees
Labels
area/libs > error-stack Affects the `error-stack` crate (library) area/libs Relates to first-party libraries/crates/packages (area) category/bug Something isn't working lang/rust Pull requests that update Rust code state/duplicate This issue or pull request already exists

Comments

@QAston
Copy link

QAston commented Apr 25, 2024

Describe the bug

Report::from currently ignores nested .source() results of std::error::Error-compliant types.

For anyhow and eyre errors there's a compat module which loads their nested errors, but there's nothing like this for basic std::error::Error like the ones generated from thiserror::Error.

To reproduce

struct MyContext;
fn main() {
  let wrapped_client = reqwest::Client::new();
  let client = reqwest_middleware::ClientBuilder::new(wrapped_client).build()
  let response = client
     // nonexistent server
    .post("http://localhost:9999/")
    .body("asdf")
    .send()
    .await
    .change_context(MyContext);

  println!("{:?}", response)
}

prints error:

Error: IoError
├╴at location
│
╰─▶ Request error: error sending request for url (http://localhost:9999/)
    ├╴at location
    ╰╴backtrace with 29 frames (1)

while it shoud include nested errors along the lines of

source: Error { kind: Connect, source: Some(ConnectError("tcp connect error", Os { code: 111, kind: ConnectionRefused, message: "Connection refused" })) }

Expected behavior

Errors that have std::error::Error::source() implemented in a nested manner, like reqwest-midleware::Error which contains reqwest::Error should have their sources displayed recursively in the display output.

Recursive nesting of errors is the currently default paradigm of nesting errors in the rust ecosystem and should be supported.

Example:

Error: MyContext
├╴at location

╰─▶ Request error: error sending request for url (http://localhost:9999/)
├╴backtrace with 28 frames (1)
├╴at location
├╴error sending request for url (http://localhost:9999/)
├╴client error (Connect)
├╴tcp connect error: Connection refused (os error 111)
╰╴Connection refused (os error 111)

The above was achieved using this abomination:

fn main() {
  let wrapped_client = reqwest::Client::new();
  let client = reqwest_middleware::ClientBuilder::new(wrapped_client).build()
  let response = client
    .post("http://localhost:9999/")
    .body("asdf")
    .send()
    .await
    .map_err(|e| anyhow::anyhow!(e))
    // result of IntoReportCompat::into_report doesn't allow for .change_context(), so we map_err ...
    .into_report().map_err(|e|e.change_context(Error::IoError))?
  println!("{:?}", response);
}

Rust compiler

rustc 1.78.0-nightly (5119208fd 2024-03-02)

Host

x86_64-unknown-linux-gnu

Target

x86_64-unknown-linux-gnu

Version

v0.4.1

Features

default

Additional context

No response

@QAston QAston added area/libs > error-stack Affects the `error-stack` crate (library) category/bug Something isn't working labels Apr 25, 2024
@QAston QAston changed the title Report::from should load .source() information from std::error::Error-compatible errors Report::from should load recursive .source() information from std::error::Error-compatible errors Apr 25, 2024
@vilkinsons vilkinsons added area/libs Relates to first-party libraries/crates/packages (area) lang/rust Pull requests that update Rust code labels Apr 25, 2024
@vilkinsons vilkinsons changed the title Report::from should load recursive .source() information from std::error::Error-compatible errors GEN-152: report::from should load recursive .source() information from std::error::Error-compatible errors Apr 27, 2024
@TimDiekmann TimDiekmann closed this as not planned Won't fix, can't repro, duplicate, stale Apr 27, 2024
@TimDiekmann TimDiekmann reopened this Apr 27, 2024
@TimDiekmann
Copy link
Member

Duplicate of #2127

@TimDiekmann TimDiekmann marked this as a duplicate of #2127 Apr 27, 2024
@TimDiekmann TimDiekmann added the state/duplicate This issue or pull request already exists label Apr 27, 2024
@TimDiekmann
Copy link
Member

Hi @QAston and thanks for filing this issue. I absolutely hear you and agree that the sources should be captured. I marked this as a duplicate of #2127 as this was flagged before. The main issue with that is that source does not return an error which is Send + Sync. Ideally, we would print the sources as attachments.

One way to get a similar result now is to attach the error as string and revisit this at some point later. Happy to hear your thoughts but I'd prefer to keep the discussion in one issue 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/libs > error-stack Affects the `error-stack` crate (library) area/libs Relates to first-party libraries/crates/packages (area) category/bug Something isn't working lang/rust Pull requests that update Rust code state/duplicate This issue or pull request already exists
Development

No branches or pull requests

4 participants