Skip to content

Commit

Permalink
Revamp errors in aws-smithy-eventstream
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Nov 2, 2022
1 parent 55c1535 commit aa06082
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -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
)))
Expand All @@ -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,
)
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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()),
));
""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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,
Expand Down Expand Up @@ -212,7 +212,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,
Expand Down
40 changes: 36 additions & 4 deletions rust-runtime/aws-smithy-eventstream/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use aws_smithy_types::DateTime;
use std::error::Error as StdError;
use std::fmt;

#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
pub(crate) enum ErrorKind {
HeadersTooLong,
HeaderValueTooLong,
InvalidHeaderNameLength,
Expand All @@ -27,12 +26,45 @@ pub enum Error {
Unmarshalling(String),
}

#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}

impl Error {
// Used in tests to match on the underlying error kind
#[cfg(test)]
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<String>) -> 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<String>) -> Self {
Self {
kind: ErrorKind::Unmarshalling(message.into()),
}
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Error { kind }
}
}

impl StdError for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
match self {
use ErrorKind::*;
match &self.kind {
HeadersTooLong => write!(f, "headers too long to fit in event stream frame"),
HeaderValueTooLong => write!(f, "header value too long to fit in event stream frame"),
InvalidHeaderNameLength => write!(f, "invalid header name length"),
Expand Down
Loading

0 comments on commit aa06082

Please sign in to comment.