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

Rework Error type #583

Merged
merged 3 commits into from
Sep 14, 2023
Merged

Rework Error type #583

merged 3 commits into from
Sep 14, 2023

Conversation

danielocfb
Copy link
Collaborator

Our existing Error type is sub-optimal in many ways:

  1. using an exhaustive enum we can't easily extend it without breaking
    backwards compatibility
  2. the provided variants are rather subjective and not useful: what is a
    system error? Why is it relevant that something is a system error vs.
    something else? What could be more relevant to users, would be the
    kind of error encountered, e.g., permission denied or object not
    found.
    Similarly, what is an internal error? If an internal invariant is
    violated that's a bug, but that's not how most sites use this
    variant.
  3. it is impossible to chain errors, reporting lower level errors (such
    as file not found) along with higher level context (e.g., file name).

This change proposes to rework the Error type completely. The type is very close to io::Error, just by virtue of us doing a lot of IO and interfacing with system-level APIs (most of which effectively report io::Error objects) and so it is a natural fit. We certainly mirror several of the std::io::ErrorKind variants in our own version of ErrorKind. On top of the error itself we also add a bunch of convenience conversion functions, mostly to provide additional context. These are heavily inspired by anyhow's Error type. That crate also acted as role model for the way we format our errors.
This change only introduces the error type in a new module, error2. A subsequent one will adjust all internal consumers to use it and remove the existing Error enum.

@danielocfb danielocfb marked this pull request as draft September 11, 2023 17:04
@danielocfb danielocfb force-pushed the topic/error branch 3 times, most recently from a14a62c to 5d8c431 Compare September 11, 2023 17:12
@danielocfb danielocfb marked this pull request as ready for review September 11, 2023 17:13
Our existing Error type is sub-optimal in many ways:
1) using an exhaustive enum we can't easily extend it without breaking
   backwards compatibility
2) the provided variants are rather subjective and not useful: what is a
   system error? Why is it relevant that something is a system error vs.
   something else? What could be more relevant to users, would be the
   kind of error encountered, e.g., permission denied or object not
   found.
   Similarly, what is an internal error? If an internal invariant is
   violated that's a bug, but that's not how most sites use this
   variant.
3) it is impossible to chain errors, reporting lower level errors (such
   as file not found) along with higher level context (e.g., file name).

This change proposes to rework the Error type completely. The type is
very close to io::Error, just by virtue of us doing a lot of IO and
interfacing with system-level APIs (most of which effectively report
io::Error objects) and so it is a natural fit. We certainly mirror
several of the std::io::ErrorKind variants in our own version of
ErrorKind. On top of the error itself we also add a bunch of convenience
conversion functions, mostly to provide additional context. These are
heavily inspired by anyhow's Error type. That crate also acted as role
model for the way we format our errors.
This change only introduces the error type in a new module, error2. A
subsequent one will adjust all internal consumers to use it and remove
the existing Error enum.

Signed-off-by: Daniel Müller <[email protected]>
This change replaces all call-sites using the previous enum based Error
with the new version. For the most part it is a straight-forward
conversion.

Signed-off-by: Daniel Müller <[email protected]>
By now we have adjusted all users of crate-level errors to use the new
Error type instead of the old one. With this change we conclude the
switch by removing the old type altogether.

Signed-off-by: Daniel Müller <[email protected]>
Copy link
Member

@anakryiko anakryiko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the justification make sense

@danielocfb danielocfb merged commit 2196f44 into libbpf:master Sep 14, 2023
10 checks passed
@danielocfb danielocfb deleted the topic/error branch September 14, 2023 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants