Skip to content

RFC30: serde support for error metadata #2637

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions rust-runtime/aws-smithy-types/src/error/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ pub const EMPTY_ERROR_METADATA: ErrorMetadata = ErrorMetadata {
/// For many services, Errors are modeled. However, many services only partially model errors or don't
/// model errors at all. In these cases, the SDK will return this generic error type to expose the
/// `code`, `message` and `request_id`.
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-serialize"),
derive(serde::Serialize)
)]
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-deserialize"),
derive(serde::Deserialize)
)]
#[derive(Debug, Eq, PartialEq, Default, Clone)]
pub struct ErrorMetadata {
code: Option<String>,
Expand All @@ -53,8 +61,23 @@ impl ProvideErrorMetadata for ErrorMetadata {
}

/// Builder for [`ErrorMetadata`].
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-serialize"),
derive(serde::Serialize)
)]
#[cfg_attr(
all(aws_sdk_unstable, feature = "serde-deserialize"),
derive(serde::Deserialize)
)]
#[derive(Debug, Default)]
pub struct Builder {
#[cfg_attr(
any(
all(aws_sdk_unstable, feature = "serde-deserialize"),
all(aws_sdk_unstable, feature = "serde-serialize")
),
serde(flatten)
)]
inner: ErrorMetadata,
}

Expand Down Expand Up @@ -170,3 +193,36 @@ impl fmt::Display for ErrorMetadata {
}

impl std::error::Error for ErrorMetadata {}

#[cfg(all(
test,
any(
all(aws_sdk_unstable, feature = "serde-deserialize"),
all(aws_sdk_unstable, feature = "serde-serialize")
)
))]
mod test {
use super::*;

#[test]
/// tests de/ser on ErrorMetaData.
fn test_error_meta_data() {
let mut data = Builder::default()
.code("code")
.message("message")
.custom("hello", "world");
let ok = serde_json::to_string_pretty(&EMPTY_ERROR_METADATA).unwrap();
assert_eq!(
&ok,
include_str!("../../test-data/error_meta_data_empty.json")
);
assert_eq!(
serde_json::from_str(include_str!("../../test-data/error_meta_data.json")).unwrap(),
&data
);
assert_eq!(
serde_json::from_str(include_str!("../../test-data/error_meta_data.json")).unwrap(),
data.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"code": "code",
"message": "message",
"extras": {
"hello": "world"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"code": null,
"message": null,
"extras": null
}