Skip to content

RUST-1406 Add type-specific errors to standard error type #555

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

Merged
merged 13 commits into from
Jun 18, 2025

Conversation

isabelatkinson
Copy link
Contributor

@isabelatkinson isabelatkinson commented Jun 13, 2025

This maintains the existing structure of error types that have information that could be used programatically, e.g. oid::Error. I'm not sure how much users are actually using those values outside of printing them, but I'd rather not change that API too much unnecessarily. Type-specific errors that had a string message for each variant are collapsed into a single crate::error::Error variant for their type.

@isabelatkinson isabelatkinson marked this pull request as ready for review June 16, 2025 16:15
@isabelatkinson isabelatkinson requested a review from a team as a code owner June 16, 2025 16:15
@isabelatkinson isabelatkinson requested a review from abr-egn June 16, 2025 16:15
@isabelatkinson isabelatkinson marked this pull request as draft June 16, 2025 16:16
@isabelatkinson isabelatkinson removed the request for review from abr-egn June 16, 2025 16:17
@isabelatkinson
Copy link
Contributor Author

going to change a few small things - will re-request review when this is ready again!

@isabelatkinson isabelatkinson marked this pull request as ready for review June 16, 2025 16:58
#[non_exhaustive]
pub enum ErrorKind {
/// An error related to the [`Binary`](crate::Binary) type occurred.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is exposing how arbitrary it is which of our errors carry a string and which carry a subkind enum; that's got a bit of a code smell to it but I can't decide if it's actually a problem worth addressing. Do you have thoughts here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't love this design; I agree that it's arbitrary and inconsistent. My concern, however, is that users who do have some kind of programmatic use for the values in these errors will be less inclined to migrate to the new version, so my instinct here is to err on the side of portability for lower-priority changes. Certainly open to other designs here if you had something else in mind though!

Copy link
Contributor

Choose a reason for hiding this comment

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

Sketch of a proposal:

  • Hoist message to an optional field of Error, with corresponding code in the Display impl to append it if it's present.
  • All variants of ErrorKind are required to be #[non_exhaustive] struct variants, even if they're empty (which a lot now will be with message stripped out)
  • Any sub-Kind types follow the same pattern (sub-Kinds also don't need to have message since they're ultimately attached to an Error!)

This makes the error space a lot more uniform, gives us a lot of flexibility for future error evolution, and doesn't introduce substantial migration burden.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this a lot more - updated with your suggestions!

Io(std::io::Error),
#[error("An IO error occurred")]
#[non_exhaustive]
Io {},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

switched this over to a stringified version of the error - std::io::Error is not Clone, and although I'm not worried about the stability of a standard library error, keeping external types out of our API allows for more flexibility in situations like RUST-1798

Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to carry the std::io::ErrorKind but that can be added later if it's asked for :)

#[non_exhaustive]
pub enum ErrorKind {
/// An error related to the [`Binary`](crate::Binary) type occurred.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this a lot more - updated with your suggestions!

Copy link
Contributor

@abr-egn abr-egn left a comment

Choose a reason for hiding this comment

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

LGTM!

Io(std::io::Error),
#[error("An IO error occurred")]
#[non_exhaustive]
Io {},
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to carry the std::io::ErrorKind but that can be added later if it's asked for :)

@isabelatkinson isabelatkinson merged commit 3cbf444 into mongodb:main Jun 18, 2025
11 checks passed
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.

2 participants