Skip to content

Commit

Permalink
Remove RuntimeErrorKind
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Barber committed Oct 6, 2022
1 parent d46debe commit 7793800
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ open class ServerOperationHandlerGenerator(
Ok(v) => v,
Err(extension_not_found_rejection) => {
let extension = $serverCrate::extension::RuntimeErrorExtension::new(extension_not_found_rejection.to_string());
let runtime_error = $serverCrate::runtime_error::RuntimeError { kind: extension_not_found_rejection.into() };
let runtime_error = $serverCrate::runtime_error::RuntimeError::from(extension_not_found_rejection);
let mut response = #{SmithyHttpServer}::response::IntoResponse::<#{Protocol}>::into_response(runtime_error);
response.extensions_mut().insert(extension);
return response.map($serverCrate::body::boxed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
rustTemplate(
"""
if ! #{SmithyHttpServer}::protocols::accept_header_classifier(req, ${contentType.dq()}) {
return Err(#{RuntimeError} {
kind: #{SmithyHttpServer}::runtime_error::RuntimeErrorKind::NotAcceptable,
})
return Err(#{RuntimeError}::NotAcceptable)
}
""",
*codegenScope,
Expand All @@ -198,9 +196,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
rustTemplate(
"""
if #{SmithyHttpServer}::protocols::content_type_header_classifier(req, $expectedRequestContentType).is_err() {
return Err(#{RuntimeError} {
kind: #{SmithyHttpServer}::runtime_error::RuntimeErrorKind::UnsupportedMediaType,
})
return Err(#{RuntimeError}::UnsupportedMediaType)
}
""",
*codegenScope,
Expand All @@ -227,11 +223,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
#{parse_request}(req)
.await
.map($inputName)
.map_err(
|err| #{RuntimeError} {
kind: err.into()
}
)
.map_err(Into::into)
}
}
Expand Down Expand Up @@ -278,7 +270,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
Self::Output(o) => {
match #{serialize_response}(o) {
Ok(response) => response,
Err(e) => #{SmithyHttpServer}::response::IntoResponse::<#{Marker}>::into_response(#{RuntimeError} { kind: e.into() })
Err(e) => #{SmithyHttpServer}::response::IntoResponse::<#{Marker}>::into_response(#{RuntimeError}::from(e))
}
},
Self::Error(err) => {
Expand All @@ -287,7 +279,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
response.extensions_mut().insert(#{SmithyHttpServer}::extension::ModeledErrorExtension::new(err.name()));
response
},
Err(e) => #{SmithyHttpServer}::response::IntoResponse::<#{Marker}>::into_response(#{RuntimeError} { kind: e.into() })
Err(e) => #{SmithyHttpServer}::response::IntoResponse::<#{Marker}>::into_response(#{RuntimeError}::from(e))
}
}
}
Expand Down Expand Up @@ -332,9 +324,7 @@ private class ServerHttpBoundProtocolTraitImplGenerator(
"""
match #{serialize_response}(self.0) {
Ok(response) => response,
Err(e) => #{SmithyHttpServer}::response::IntoResponse::<#{Marker}>::into_response(#{RuntimeError} {
kind: e.into()
})
Err(e) => #{SmithyHttpServer}::response::IntoResponse::<#{Marker}>::into_response(#{RuntimeError}::from(e))
}
""".trimIndent()

Expand Down
73 changes: 30 additions & 43 deletions rust-runtime/aws-smithy-http-server/src/runtime_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::proto::rest_xml::AwsRestXml;
use crate::response::IntoResponse;

#[derive(Debug)]
pub enum RuntimeErrorKind {
pub enum RuntimeError {
/// Request failed to deserialize or response failed to serialize.
Serialization(crate::Error),
/// As of writing, this variant can only occur upon failure to extract an
Expand All @@ -45,22 +45,22 @@ pub enum RuntimeErrorKind {
/// String representation of the runtime error type.
/// Used as the value of the `X-Amzn-Errortype` header in RestJson1.
/// Used as the value passed to construct an [`crate::extension::RuntimeErrorExtension`].
impl RuntimeErrorKind {
impl RuntimeError {
pub fn name(&self) -> &'static str {
match self {
RuntimeErrorKind::Serialization(_) => "SerializationException",
RuntimeErrorKind::InternalFailure(_) => "InternalFailureException",
RuntimeErrorKind::NotAcceptable => "NotAcceptableException",
RuntimeErrorKind::UnsupportedMediaType => "UnsupportedMediaTypeException",
Self::Serialization(_) => "SerializationException",
Self::InternalFailure(_) => "InternalFailureException",
Self::NotAcceptable => "NotAcceptableException",
Self::UnsupportedMediaType => "UnsupportedMediaTypeException",
}
}

pub fn status_code(&self) -> StatusCode {
match self {
RuntimeErrorKind::Serialization(_) => StatusCode::BAD_REQUEST,
RuntimeErrorKind::InternalFailure(_) => StatusCode::INTERNAL_SERVER_ERROR,
RuntimeErrorKind::NotAcceptable => StatusCode::NOT_ACCEPTABLE,
RuntimeErrorKind::UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
Self::Serialization(_) => StatusCode::BAD_REQUEST,
Self::InternalFailure(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::NotAcceptable => StatusCode::NOT_ACCEPTABLE,
Self::UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
}
}
}
Expand All @@ -69,48 +69,35 @@ pub struct InternalFailureException;

impl IntoResponse<AwsJson10> for InternalFailureException {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
IntoResponse::<AwsJson10>::into_response(RuntimeError {
kind: RuntimeErrorKind::InternalFailure(crate::Error::new(String::new())),
})
IntoResponse::<AwsJson10>::into_response(RuntimeError::InternalFailure(crate::Error::new(String::new())))
}
}

impl IntoResponse<AwsJson11> for InternalFailureException {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
IntoResponse::<AwsJson11>::into_response(RuntimeError {
kind: RuntimeErrorKind::InternalFailure(crate::Error::new(String::new())),
})
IntoResponse::<AwsJson11>::into_response(RuntimeError::InternalFailure(crate::Error::new(String::new())))
}
}

impl IntoResponse<AwsRestJson1> for InternalFailureException {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
IntoResponse::<AwsRestJson1>::into_response(RuntimeError {
kind: RuntimeErrorKind::InternalFailure(crate::Error::new(String::new())),
})
IntoResponse::<AwsRestJson1>::into_response(RuntimeError::InternalFailure(crate::Error::new(String::new())))
}
}

impl IntoResponse<AwsRestXml> for InternalFailureException {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
IntoResponse::<AwsRestXml>::into_response(RuntimeError {
kind: RuntimeErrorKind::InternalFailure(crate::Error::new(String::new())),
})
IntoResponse::<AwsRestXml>::into_response(RuntimeError::InternalFailure(crate::Error::new(String::new())))
}
}

#[derive(Debug)]
pub struct RuntimeError {
pub kind: RuntimeErrorKind,
}

impl IntoResponse<AwsRestJson1> for RuntimeError {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
http::Response::builder()
.status(self.kind.status_code())
.status(self.status_code())
.header("Content-Type", "application/json")
.header("X-Amzn-Errortype", self.kind.name())
.extension(RuntimeErrorExtension::new(self.kind.name().to_string()))
.header("X-Amzn-Errortype", self.name())
.extension(RuntimeErrorExtension::new(self.name().to_string()))
// See https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_1-protocol.html#empty-body-serialization
.body(crate::body::to_boxed("{}"))
.expect("invalid HTTP response for `RuntimeError`; please file a bug report under https://github.com/awslabs/smithy-rs/issues")
Expand All @@ -120,9 +107,9 @@ impl IntoResponse<AwsRestJson1> for RuntimeError {
impl IntoResponse<AwsRestXml> for RuntimeError {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
http::Response::builder()
.status(self.kind.status_code())
.status(self.status_code())
.header("Content-Type", "application/xml")
.extension(RuntimeErrorExtension::new(self.kind.name().to_string()))
.extension(RuntimeErrorExtension::new(self.name().to_string()))
.body(crate::body::to_boxed(""))
.expect("invalid HTTP response for `RuntimeError`; please file a bug report under https://github.com/awslabs/smithy-rs/issues")
}
Expand All @@ -131,9 +118,9 @@ impl IntoResponse<AwsRestXml> for RuntimeError {
impl IntoResponse<AwsJson10> for RuntimeError {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
http::Response::builder()
.status(self.kind.status_code())
.status(self.status_code())
.header("Content-Type", "application/x-amz-json-1.0")
.extension(RuntimeErrorExtension::new(self.kind.name().to_string()))
.extension(RuntimeErrorExtension::new(self.name().to_string()))
// See https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_0-protocol.html#empty-body-serialization
.body(crate::body::to_boxed(""))
.expect("invalid HTTP response for `RuntimeError`; please file a bug report under https://github.com/awslabs/smithy-rs/issues")
Expand All @@ -143,32 +130,32 @@ impl IntoResponse<AwsJson10> for RuntimeError {
impl IntoResponse<AwsJson11> for RuntimeError {
fn into_response(self) -> http::Response<crate::body::BoxBody> {
http::Response::builder()
.status(self.kind.status_code())
.status(self.status_code())
.header("Content-Type", "application/x-amz-json-1.1")
.extension(RuntimeErrorExtension::new(self.kind.name().to_string()))
.extension(RuntimeErrorExtension::new(self.name().to_string()))
// See https://awslabs.github.io/smithy/1.0/spec/aws/aws-json-1_1-protocol.html#empty-body-serialization
.body(crate::body::to_boxed(""))
.expect("invalid HTTP response for `RuntimeError`; please file a bug report under https://github.com/awslabs/smithy-rs/issues")
}
}

impl From<crate::rejection::RequestExtensionNotFoundRejection> for RuntimeErrorKind {
impl From<crate::rejection::RequestExtensionNotFoundRejection> for RuntimeError {
fn from(err: crate::rejection::RequestExtensionNotFoundRejection) -> Self {
RuntimeErrorKind::InternalFailure(crate::Error::new(err))
Self::InternalFailure(crate::Error::new(err))
}
}

impl From<crate::rejection::ResponseRejection> for RuntimeErrorKind {
impl From<crate::rejection::ResponseRejection> for RuntimeError {
fn from(err: crate::rejection::ResponseRejection) -> Self {
RuntimeErrorKind::Serialization(crate::Error::new(err))
Self::Serialization(crate::Error::new(err))
}
}

impl From<crate::rejection::RequestRejection> for RuntimeErrorKind {
impl From<crate::rejection::RequestRejection> for RuntimeError {
fn from(err: crate::rejection::RequestRejection) -> Self {
match err {
crate::rejection::RequestRejection::MissingContentType(_reason) => RuntimeErrorKind::UnsupportedMediaType,
_ => RuntimeErrorKind::Serialization(crate::Error::new(err)),
crate::rejection::RequestRejection::MissingContentType(_reason) => Self::UnsupportedMediaType,
_ => Self::Serialization(crate::Error::new(err)),
}
}
}

0 comments on commit 7793800

Please sign in to comment.