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

Revise unhandled error variant according to RFC-39 #3191

Merged
merged 16 commits into from
Nov 15, 2023

Conversation

jdisanti
Copy link
Collaborator

@jdisanti jdisanti commented Nov 14, 2023

This PR implements RFC-39 with a couple slight deviations:

  • No introspect method is added since Error already implements ProvideErrorMetadata.
  • The same opaqueness and deprecation pointer is applied to the enum unknown variant for consistency.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@jdisanti jdisanti added breaking-change This will require a breaking change needs-sdk-review labels Nov 14, 2023
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

Base automatically changed from jdisanti-error-meta-service-error to main November 14, 2023 19:40
@jdisanti jdisanti force-pushed the jdisanti-unhandled-error-refactor branch from a71467e to b308867 Compare November 14, 2023 21:47
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@jdisanti jdisanti marked this pull request as ready for review November 14, 2023 22:10
@jdisanti jdisanti requested review from a team as code owners November 14, 2023 22:10
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

Copy link
Collaborator

@rcoh rcoh left a comment

Choose a reason for hiding this comment

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

approved with a few comments—I think we should also workshop that deprecation message but we can do that later

@@ -36,6 +37,7 @@ impl RequestIdExt for ErrorMetadata {
}
}

#[allow(deprecated)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess this is where I'd consider using Introspect but this is fine, I don't think it's a huge deal if we do it this way.

// An unexpected error occurred (e.g., invalid JSON returned by the service or an unknown error code).
GetStorageError::Unhandled(uh) => {
tracing::error!(error = %uh, "An unhandled error has occurred.")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

if this is an example it should probably use DisplayErrorContext

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call. Fixed.

@@ -58,6 +61,7 @@ impl Builder {
/// [`DisplayErrorContext`](crate::error::display::DisplayErrorContext), use another
/// error reporter library that visits the error's cause/source chain, or call
/// [`Error::source`](std::error::Error::source) for more details about the underlying cause.
#[deprecated(note = "This type is no longer used by errors.")]
Copy link
Collaborator

Choose a reason for hiding this comment

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

so is this type still used at all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's no longer used. I left it in to be removed in the next release in case anyone was referencing it.

```rust
match service_error.err() {
GetStorageError::StorageAccessNotAuthorized(_) => { /* ... */ }
GetStorageError::Unhandled(unhandled) if unhandled.code() == "SomeUnmodeledErrorCode" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

code is optional; == Some(...), same for below

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch!

@jdisanti
Copy link
Collaborator Author

jdisanti commented Nov 15, 2023

While fixing examples for this, I realized we need a way to parse an enum while disallowing unknown variants. For example, this is what examples are currently doing:

    // parse resource type from user input
    let parsed = ResourceType::from(resource_type.as_str());
    if matches!(parsed, ResourceType::Unknown(_)) {
        panic!(
            "unknown resource type: `{}`. Valid resource types: {:#?}",
            &resource_type,
            ResourceType::values()
        )
    }

I'm going to add a try_parse method to the enums for this use-case that will error if the variant doesn't exist, which will make that look as follows:

    // parse resource type from user input
    let parsed = match ResourceType::try_parse(resource_type.as_str()) {
        Ok(parsed) => parsed,
        Err(_) => panic!(
            "unknown resource type: `{}`. Valid resource types: {:#?}",
            &resource_type,
            ResourceType::values()
        ),
    };

@jdisanti
Copy link
Collaborator Author

Also found that StatusCode was inaccessible while fixing examples. Added a fix here in 34b3dea.

Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@rcoh
Copy link
Collaborator

rcoh commented Nov 15, 2023

LGTM. Suggested deprecation message:

#[deprecated(
    note = "Matching `Unhandled` directly is not forwards compatible. Instead, match using a 
    variable wildcard pattern and check `.code()`: 
       `err if err.code() == Some(\"SpecificExceptionCode\") => { /* handle the error */ }`.
    See [`ProvideErrorMetadata`](#impl-Unhandled-for-<NameOfStruct>) for more information."
)]
Screenshot 2023-11-15 at 10 46 29 AM

It renders like this (and the link works!)

@jdisanti
Copy link
Collaborator Author

Made a slight tweak to your suggested message:

See [`ProvideErrorMetadata`]($link) for what information is available for the error.

Instead of "for more information" since the trait doesn't give more information about this deprecation, so that could have been confusing.

Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@jdisanti jdisanti merged commit c830caa into main Nov 15, 2023
40 of 41 checks passed
@jdisanti jdisanti deleted the jdisanti-unhandled-error-refactor branch November 15, 2023 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change This will require a breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants