|
| 1 | +<!-- Give your RFC a descriptive name saying what it would accomplish or what feature it defines --> |
| 2 | +RFC: Forward Compatible Errors |
| 3 | +============= |
| 4 | + |
| 5 | +<!-- RFCs start with the "RFC" status and are then either "Implemented" or "Rejected". --> |
| 6 | +> Status: RFC |
| 7 | +> |
| 8 | +> Applies to: client |
| 9 | +
|
| 10 | +<!-- A great RFC will include a list of changes at the bottom so that the implementor can be sure they haven't missed anything --> |
| 11 | +For a summarized list of proposed changes, see the [Changes Checklist](#changes-checklist) section. |
| 12 | + |
| 13 | +<!-- Insert a short paragraph explaining, at a high level, what this RFC is for --> |
| 14 | +This RFC defines an approach for making it forwards-compatible to convert **unmodeled** `Unhandled` errors into modeled ones. This occurs as servers update their models to include errors that were previously unmodeled. |
| 15 | + |
| 16 | +Currently, SDK errors are **not** forward compatible in this way. If a customer matches `Unhandled` in addition to the `_` branch and a new variant is added, **they will fail to match the new variant**. We currently handle this issue with enums by prevent useful information from being readable from the `Unknown` variant. |
| 17 | + |
| 18 | +This is related to ongoing work on the [`non_exhaustive_omitted_patterns` lint](https://github.com/rust-lang/rust/issues/89554) which would produce a compiler warning when a new variant was added even when `_` was used. |
| 19 | + |
| 20 | +<!-- The "Terminology" section is optional but is really useful for defining the technical terms you're using in the RFC --> |
| 21 | +Terminology |
| 22 | +----------- |
| 23 | + |
| 24 | +For purposes of discussion, consider the following error: |
| 25 | +```rust,ignore |
| 26 | +#[non_exhaustive] |
| 27 | +pub enum AbortMultipartUploadError { |
| 28 | + NoSuchUpload(NoSuchUpload), |
| 29 | + Unhandled(Unhandled), |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +- **Modeled Error**: An error with an named variant, e.g. `NoSuchUpload` above |
| 34 | +- **Unmodeled Error**: Any other error, e.g. if the server returned `ValidationException` for the above operation. |
| 35 | +- **Error code**: All errors across all protocols provide a `code`, a unique method to identify an error across the service closure. |
| 36 | + |
| 37 | +<!-- Explain how users will use this new feature and, if necessary, how this compares to the current user experience --> |
| 38 | +The user experience if this RFC is implemented |
| 39 | +---------------------------------------------- |
| 40 | + |
| 41 | +In the current version of the SDK, users match the `Unhandled` variant. They can then read the code from the `Unhandled` variant because [`Unhandled`](https://docs.rs/aws-smithy-types/0.56.1/aws_smithy_types/error/struct.Unhandled.html) implements the `ProvideErrorMetadata` trait as well as the standard-library `std::error::Error` trait. |
| 42 | + |
| 43 | +> Note: It's possible to write correct code today because the operation-level and service-level errors already expose `code()` via `ProvideErrorMetadata`. This RFC describes mechanisms to guide customers to write forward-compatible code. |
| 44 | +
|
| 45 | +```rust,ignore |
| 46 | +# fn docs() { |
| 47 | + match client.get_object().send().await { |
| 48 | + Ok(obj) => { ... }, |
| 49 | + Err(e) => match e.into_service_error() { |
| 50 | + GetObjectError::NotFound => { ... }, |
| 51 | + GetObjectError::Unhandled(err) if err.code() == "ValidationException" => { ... } |
| 52 | + other => { /** do something with this variant */ } |
| 53 | + } |
| 54 | + } |
| 55 | +# } |
| 56 | +``` |
| 57 | + |
| 58 | +We must instead guide customers into the following pattern: |
| 59 | +```rust,ignore |
| 60 | +# fn docs() { |
| 61 | + match client.get_object().send().await { |
| 62 | + Ok(obj) => { ... }, |
| 63 | + Err(e) => match e.into_service_error() { |
| 64 | + GetObjectError::NotFound => { ... }, |
| 65 | + err if err.code() == "ValidationException" => { ... }, |
| 66 | + err => warn!("{}", err.code()), |
| 67 | + } |
| 68 | + } |
| 69 | +# } |
| 70 | +``` |
| 71 | + |
| 72 | +In this example, because customers are _not_ matching on the `Unhandled` variant explicitly this code is forward compatible for `ValidationException` being introduced in the future. |
| 73 | + |
| 74 | +**Guiding Customers to this Pattern** |
| 75 | +There are two areas we need to handle: |
| 76 | +1. Prevent customers from extracting useful information from `Unhandled` |
| 77 | +2. Alert customers _currently_ using unhandled what to use instead. For example, the following code is still problematic: |
| 78 | + ```rust,ignore |
| 79 | + match err { |
| 80 | + GetObjectError::NotFound => { ... }, |
| 81 | + err @ GetObject::Unhandled(_) if err.code() == Some("ValidationException") => { ... } |
| 82 | + } |
| 83 | + ``` |
| 84 | +
|
| 85 | +For `1`, we need to remove the `ProvideErrorMetadata` trait implementation from `Unhandled`. We would expose this isntead through a layer of indirection to enable code generated to code to still read the data. |
| 86 | +
|
| 87 | +For `2`, we would deprecate the `Unhandled` variants with a message clearly indicating how this code should be written. |
| 88 | +
|
| 89 | +How to actually implement this RFC |
| 90 | +---------------------------------- |
| 91 | +
|
| 92 | +### Locking down `Unhandled` |
| 93 | +In order to prevent accidental matching on `Unhandled`, we need to make it hard to extract useful information from `Unhandled` itself. We will do this by removing the `ProvideErrorMetadata` trait implementation and exposing the following method: |
| 94 | +
|
| 95 | +```rust,ignore |
| 96 | +#[doc(hidden)] |
| 97 | +/// Introspect the error metadata of this error. |
| 98 | +/// |
| 99 | +/// This method should NOT be used from external code because matching on `Unhandled` directly is a backwards-compatibility |
| 100 | +/// hazard. See `RFC-0039` for more information. |
| 101 | +pub fn introspect(&self) -> impl ProvideErrorMetadata + '_ { |
| 102 | + struct Introspected<'a>(&'a Unhandled); |
| 103 | + impl ProvideErrorMetadata for Introspected { ... } |
| 104 | + Introspected(self) |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +Generated code would this use `introspect` when supporting **top-level** `ErrorMetadata` (e.g. for [`aws_sdk_s3::Error`](https://docs.rs/aws-sdk-s3/latest/aws_sdk_s3/enum.Error.html)). |
| 109 | + |
| 110 | +### Deprecating the Variant |
| 111 | +The `Unhandled` variant will be deprecated to prevent users from matching on it inadvertently. |
| 112 | + |
| 113 | +```rust,ignore |
| 114 | +enum GetObjectError { |
| 115 | + NotFound(NotFound), |
| 116 | + #[deprecated("Matching on `Unhandled` directly is a backwards compatibility hazard. Use `err if err.error_code() == ...` instead. See [here](<docs about using errors>) for more information.")] |
| 117 | + Unhandled(Unhandled) |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### |
| 122 | + |
| 123 | +<!-- Include a checklist of all the things that need to happen for this RFC's implementation to be considered complete --> |
| 124 | +Changes checklist |
| 125 | +----------------- |
| 126 | + |
| 127 | +- [ ] Generate code to deprecate unhandled variants. Determine the best way to allow `Unhandled` to continue to be constructed in client code |
| 128 | +- [ ] Generate code to deprecate the `Unhandled` variant for the service meta-error. Consider how this interacts with non-service errors. |
| 129 | +- [ ] Update `Unhandled` to make it useless on its own and expose information via an `Introspect` doc hidden struct. |
| 130 | +- [ ] Update developer guide to address this issue. |
| 131 | +- [ ] Changelog & Upgrade Guidance |
0 commit comments