Skip to content

Errors should return source instead of displaying it #831

@Kixunil

Description

@Kixunil

Not displaying the source and returning it from the source method is better for consumers as they may want to display error sources differently (line-by-line is a popular example, even Cargo does it). We're currently displaying sources together with messages. We should change it to use source.

There's one issue though: no_std would lose sources which may be annoying. I was previously ignoring this issue in my projects but I came up with a hopefully reasonable solution: continue displaying the source in no_std builds. This looks a bit backwards ("cargo features are supposed to be additive") but shouldn't break things unless people are foolishly matching against strings.

Another issue is adding cfgs could be annoying but I believe it can be easily solved with a macro. Something like this:

match error {
    Error::Foo(source) => write_err!(f, source, "some message"),
    Error::Bar(details, source) => write_err!(f, source, "the thing {} went wrong", details),
}

Perhaps it'd be even easier with something like amplify that could generate it together with std Error impl. Cc @dr-orlovsky

Metadata

Metadata

Assignees

No one assigned

    Labels

    1.0Issues and PRs required or helping to stabilize the APIerror handlingIssues and PRs related to error handling, mainly error refactoring epicgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions