Skip to content

Commit 12f217b

Browse files
authored
Revamp errors in aws-smithy-eventstream (#1873)
1 parent 6aef53a commit 12f217b

File tree

7 files changed

+109
-66
lines changed

7 files changed

+109
-66
lines changed

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/parse/EventStreamUnmarshallerGenerator.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class EventStreamUnmarshallerGenerator(
125125
}
126126
rustBlock("value => ") {
127127
rustTemplate(
128-
"return Err(#{Error}::Unmarshalling(format!(\"unrecognized :message-type: {}\", value)));",
128+
"return Err(#{Error}::unmarshalling(format!(\"unrecognized :message-type: {}\", value)));",
129129
*codegenScope,
130130
)
131131
}
@@ -156,7 +156,7 @@ class EventStreamUnmarshallerGenerator(
156156
*codegenScope,
157157
)
158158
false -> rustTemplate(
159-
"return Err(#{Error}::Unmarshalling(format!(\"unrecognized :event-type: {}\", _unknown_variant)));",
159+
"return Err(#{Error}::unmarshalling(format!(\"unrecognized :event-type: {}\", _unknown_variant)));",
160160
*codegenScope,
161161
)
162162
}
@@ -250,7 +250,7 @@ class EventStreamUnmarshallerGenerator(
250250
"""
251251
let content_type = response_headers.content_type().unwrap_or_default();
252252
if content_type != ${contentType.dq()} {
253-
return Err(#{Error}::Unmarshalling(format!(
253+
return Err(#{Error}::unmarshalling(format!(
254254
"expected :content-type to be '$contentType', but was '{}'",
255255
content_type
256256
)))
@@ -269,7 +269,7 @@ class EventStreamUnmarshallerGenerator(
269269
rustTemplate(
270270
"""
271271
std::str::from_utf8(message.payload())
272-
.map_err(|_| #{Error}::Unmarshalling("message payload is not valid UTF-8".into()))?
272+
.map_err(|_| #{Error}::unmarshalling("message payload is not valid UTF-8"))?
273273
""",
274274
*codegenScope,
275275
)
@@ -288,7 +288,7 @@ class EventStreamUnmarshallerGenerator(
288288
"""
289289
#{parser}(&message.payload()[..])
290290
.map_err(|err| {
291-
#{Error}::Unmarshalling(format!("failed to unmarshall $memberName: {}", err))
291+
#{Error}::unmarshalling(format!("failed to unmarshall $memberName: {}", err))
292292
})?
293293
""",
294294
"parser" to parser,
@@ -336,7 +336,7 @@ class EventStreamUnmarshallerGenerator(
336336
"""
337337
builder = #{parser}(&message.payload()[..], builder)
338338
.map_err(|err| {
339-
#{Error}::Unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err))
339+
#{Error}::unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err))
340340
})?;
341341
return Ok(#{UnmarshalledMessage}::Error(
342342
#{OpError}::new(
@@ -360,7 +360,7 @@ class EventStreamUnmarshallerGenerator(
360360
"""
361361
builder = #{parser}(&message.payload()[..], builder)
362362
.map_err(|err| {
363-
#{Error}::Unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err))
363+
#{Error}::unmarshalling(format!("failed to unmarshall ${member.memberName}: {}", err))
364364
})?;
365365
""",
366366
"parser" to parser,
@@ -394,7 +394,7 @@ class EventStreamUnmarshallerGenerator(
394394
CodegenTarget.SERVER -> {
395395
rustTemplate(
396396
"""
397-
return Err(aws_smithy_eventstream::error::Error::Unmarshalling(
397+
return Err(aws_smithy_eventstream::error::Error::unmarshalling(
398398
format!("unrecognized exception: {}", response_headers.smithy_type.as_str()),
399399
));
400400
""",

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamErrorMarshallerGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class EventStreamErrorMarshallerGenerator(
124124
rustTemplate(
125125
"""
126126
$errorName::Unhandled(_inner) => return Err(
127-
#{Error}::Marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned())
127+
#{Error}::marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned())
128128
),
129129
""",
130130
*codegenScope,

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/EventStreamMarshallerGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ open class EventStreamMarshallerGenerator(
112112
rustTemplate(
113113
"""
114114
Self::Input::${UnionGenerator.UnknownVariantName} => return Err(
115-
#{Error}::Marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned())
115+
#{Error}::marshalling(${unknownVariantError(unionSymbol.rustType().name).dq()}.to_owned())
116116
)
117117
""",
118118
*codegenScope,
@@ -212,7 +212,7 @@ open class EventStreamMarshallerGenerator(
212212
rustTemplate(
213213
"""
214214
#{serializerFn}(&$input)
215-
.map_err(|err| #{Error}::Marshalling(format!("{}", err)))?
215+
.map_err(|err| #{Error}::marshalling(format!("{}", err)))?
216216
""",
217217
"serializerFn" to serializerFn,
218218
*codegenScope,

rust-runtime/aws-smithy-eventstream/src/error.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use aws_smithy_types::DateTime;
77
use std::error::Error as StdError;
88
use std::fmt;
99

10-
#[non_exhaustive]
1110
#[derive(Debug)]
12-
pub enum Error {
11+
pub(crate) enum ErrorKind {
1312
HeadersTooLong,
1413
HeaderValueTooLong,
1514
InvalidHeaderNameLength,
@@ -27,12 +26,45 @@ pub enum Error {
2726
Unmarshalling(String),
2827
}
2928

29+
#[derive(Debug)]
30+
pub struct Error {
31+
kind: ErrorKind,
32+
}
33+
34+
impl Error {
35+
// Used in tests to match on the underlying error kind
36+
#[cfg(test)]
37+
pub(crate) fn kind(&self) -> &ErrorKind {
38+
&self.kind
39+
}
40+
41+
/// Create an `Error` for failure to marshall a message from a Smithy shape
42+
pub fn marshalling(message: impl Into<String>) -> Self {
43+
Self {
44+
kind: ErrorKind::Marshalling(message.into()),
45+
}
46+
}
47+
48+
/// Create an `Error` for failure to unmarshall a message into a Smithy shape
49+
pub fn unmarshalling(message: impl Into<String>) -> Self {
50+
Self {
51+
kind: ErrorKind::Unmarshalling(message.into()),
52+
}
53+
}
54+
}
55+
56+
impl From<ErrorKind> for Error {
57+
fn from(kind: ErrorKind) -> Self {
58+
Error { kind }
59+
}
60+
}
61+
3062
impl StdError for Error {}
3163

3264
impl fmt::Display for Error {
3365
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34-
use Error::*;
35-
match self {
66+
use ErrorKind::*;
67+
match &self.kind {
3668
HeadersTooLong => write!(f, "headers too long to fit in event stream frame"),
3769
HeaderValueTooLong => write!(f, "header value too long to fit in event stream frame"),
3870
InvalidHeaderNameLength => write!(f, "invalid header name length"),

0 commit comments

Comments
 (0)