Skip to content

Commit

Permalink
Add 'as-service-error' to simplify handling of specific error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Dec 20, 2023
1 parent 1bb0349 commit 555730a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions rust-runtime/aws-smithy-runtime-api/src/client/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,33 @@ impl<E, R> SdkError<E, R> {
}
}

/// Returns a reference underlying service error `E` if there is one
///
/// # Examples
/// ```no_run
/// # use aws_smithy_runtime_api::client::result::SdkError;
/// # #[derive(Debug)] enum GetObjectError { NoSuchKey(()), Other(()) }
/// # impl std::fmt::Display for GetObjectError {
/// # fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { unimplemented!() }
/// # }
/// # impl std::error::Error for GetObjectError {}
/// # fn example() -> Result<(), GetObjectError> {
/// # let sdk_err = SdkError::service_error(GetObjectError::NoSuchKey(()), ());
/// if sdk_err.as_service_error().map(|e|e.is_not_found()) == Some(true) {
/// println!("the object doesn't exist");
/// // return None, or handle this error specifically
/// }
/// // ... handle other error cases, happy path, etc.
/// # Ok(())
/// # }
/// ```
pub fn as_service_error(&self) -> Option<&E> {
match self {
Self::ServiceError(err) => Some(&err.source),
_ => None,
}
}

/// Converts this error into its error source.
///
/// If there is no error source, then `Err(Self)` is returned.
Expand Down

0 comments on commit 555730a

Please sign in to comment.