diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/EventStreamUnmarshallerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/EventStreamUnmarshallerGenerator.kt index 97d6bb1c832..120ced20bb5 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/EventStreamUnmarshallerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/EventStreamUnmarshallerGenerator.kt @@ -125,7 +125,7 @@ class EventStreamUnmarshallerGenerator( } rustBlock("value => ") { rustTemplate( - "return Err(#{Error}::Unmarshalling(format!(\"unrecognized :message-type: {}\", value)));", + "return Err(#{Error}::unmarshalling(format!(\"unrecognized :message-type: {}\", value)));", *codegenScope, ) } @@ -156,7 +156,7 @@ class EventStreamUnmarshallerGenerator( *codegenScope, ) false -> rustTemplate( - "return Err(#{Error}::Unmarshalling(format!(\"unrecognized :event-type: {}\", _unknown_variant)));", + "return Err(#{Error}::unmarshalling(format!(\"unrecognized :event-type: {}\", _unknown_variant)));", *codegenScope, ) } @@ -250,7 +250,7 @@ class EventStreamUnmarshallerGenerator( """ let content_type = response_headers.content_type().unwrap_or_default(); if content_type != ${contentType.dq()} { - return Err(#{Error}::Unmarshalling(format!( + return Err(#{Error}::unmarshalling(format!( "expected :content-type to be '$contentType', but was '{}'", content_type ))) @@ -269,7 +269,7 @@ class EventStreamUnmarshallerGenerator( rustTemplate( """ std::str::from_utf8(message.payload()) - .map_err(|_| #{Error}::Unmarshalling("message payload is not valid UTF-8".into()))? + .map_err(|_| #{Error}::unmarshalling("message payload is not valid UTF-8"))? """, *codegenScope, ) @@ -288,7 +288,7 @@ class EventStreamUnmarshallerGenerator( """ #{parser}(&message.payload()[..]) .map_err(|err| { - #{Error}::Unmarshalling(format!("failed to unmarshall $memberName: {}", err)) + #{Error}::unmarshalling(format!("failed to unmarshall $memberName: {}", err)) })? """, "parser" to parser, @@ -336,7 +336,7 @@ class EventStreamUnmarshallerGenerator( """ builder = #{parser}(&message.payload()[..], builder) .map_err(|err| { - #{Error}::Unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err)) + #{Error}::unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err)) })?; return Ok(#{UnmarshalledMessage}::Error( #{OpError}::new( @@ -360,7 +360,7 @@ class EventStreamUnmarshallerGenerator( """ builder = #{parser}(&message.payload()[..], builder) .map_err(|err| { - #{Error}::Unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err)) + #{Error}::unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err)) })?; """, "parser" to parser, @@ -394,7 +394,7 @@ class EventStreamUnmarshallerGenerator( CodegenTarget.SERVER -> { rustTemplate( """ - return Err(aws_smithy_eventstream::error::Error::Unmarshalling( + return Err(aws_smithy_eventstream::error::Error::unmarshalling( format!("unrecognized exception: {}", response_headers.smithy_type.as_str()), )); """, diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamErrorMarshallerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamErrorMarshallerGenerator.kt index 3d06da554c2..af7d1da7756 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamErrorMarshallerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamErrorMarshallerGenerator.kt @@ -124,7 +124,7 @@ class EventStreamErrorMarshallerGenerator( rustTemplate( """ $errorName::Unhandled(_inner) => return Err( - #{Error}::Marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned()) + #{Error}::marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned()) ), """, *codegenScope, diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamMarshallerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamMarshallerGenerator.kt index 133f2b9bb6b..b790c9588ac 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamMarshallerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamMarshallerGenerator.kt @@ -111,7 +111,7 @@ open class EventStreamMarshallerGenerator( rustTemplate( """ Self::Input::${UnionGenerator.UnknownVariantName} => return Err( - #{Error}::Marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned()) + #{Error}::marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned()) ) """, *codegenScope, @@ -211,7 +211,7 @@ open class EventStreamMarshallerGenerator( rustTemplate( """ #{serializerFn}(&$input) - .map_err(|err| #{Error}::Marshalling(format!("{}", err)))? + .map_err(|err| #{Error}::marshalling(format!("{}", err)))? """, "serializerFn" to serializerFn, *codegenScope, diff --git a/rust-runtime/aws-smithy-eventstream/src/error.rs b/rust-runtime/aws-smithy-eventstream/src/error.rs index d753a3085d1..bda5ff900d7 100644 --- a/rust-runtime/aws-smithy-eventstream/src/error.rs +++ b/rust-runtime/aws-smithy-eventstream/src/error.rs @@ -22,6 +22,7 @@ pub(crate) enum ErrorKind { PayloadTooLong, PreludeChecksumMismatch(u32, u32), TimestampValueTooLarge(DateTime), + Marshalling(String), Unmarshalling(String), } @@ -36,6 +37,20 @@ impl Error { pub(crate) fn kind(&self) -> &ErrorKind { &self.kind } + + /// Create an `Error` for failure to marshall a message from a Smithy shape + pub fn marshalling(message: impl Into) -> Self { + Self { + kind: ErrorKind::Marshalling(message.into()), + } + } + + /// Create an `Error` for failure to unmarshall a message into a Smithy shape + pub fn unmarshalling(message: impl Into) -> Self { + Self { + kind: ErrorKind::Unmarshalling(message.into()), + } + } } impl From for Error { @@ -75,6 +90,7 @@ impl fmt::Display for Error { "timestamp value {:?} is too large to fit into an i64", time ), + Marshalling(error) => write!(f, "failed to marshall message: {}", error), Unmarshalling(error) => write!(f, "failed to unmarshall message: {}", error), } }