Coder Social home page Coder Social logo

reversed printing about anyhow HOT 5 CLOSED

burrbull avatar burrbull commented on August 20, 2024
reversed printing

from anyhow.

Comments (5)

dtolnay avatar dtolnay commented on August 20, 2024

Could you show exactly what you would want this chain of errors to be printed as?

from anyhow.

burrbull avatar burrbull commented on August 20, 2024

In this example error, lowest level is (4) https://github.com/rust-embedded/svd/blob/27beb89c3eef5fbc8640b1655444a92be2888aa4/src/error.rs#L16 and I think it should be printed by Display
All other strings were added by with_context.
In peripheral PLIC - highest level of context.

Although I may be wrong.

from anyhow.

burrbull avatar burrbull commented on August 20, 2024

If it is generally accepted I close issue.

from anyhow.

dtolnay avatar dtolnay commented on August 20, 2024

I think this is behaving correctly. For example in standard library errors you can see the lowest level underlying error is not what is printed as the main representation:

use std::ffi::CString;
use std::error::Error;

fn main() {
    let cstring = CString::new(vec![255]).unwrap();
    let error = cstring.into_string().unwrap_err();
    println!("DISPLAY: {}", error);
    println!("CAUSE: {}", error.source().unwrap());
}
DISPLAY: C string contained non-utf8 bytes
CAUSE: invalid utf-8 sequence of 1 bytes from index 0

The lowest level error is "invalid utf-8 sequence of 1 bytes from index 0" while the context is "C string contained non-utf8 bytes".

If you want the cause chain printed in the opposite order, you can try printing it yourself:

use anyhow::anyhow;

fn main() {
    let err = anyhow!("Expected content in <enumeratedValues> tag, found none")
        .context("In field `priority`")
        .context("Parsing field #0")
        .context("In register `threshold`")
        .context("In cluster `targets[%s]`")
        .context("In peripheral `PLIC`");
    for err in err.chain().rev() {
        println!("{}", err);
    }
}

But I would recommend instead phrasing your contexts to make it more clear what failed:

Error: Invalid data for peripheral `PLIC` in STM32F30x.svd

Caused by:
    0: Failed to load cluster `targets[%s]` from svd
    1: Failed to load register `threshold` from svd
    2: Failed to parse field #0 of register
    3: Invalid value for field `priority`
    4: Expected content in <enumeratedValues> tag, found none

from anyhow.

burrbull avatar burrbull commented on August 20, 2024

I think this is behaving correctly. For example in standard library errors you can see the lowest level underlying error is not what is printed as the main representation:

Thank you for explanation.

from anyhow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.