Skip to content
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

DEBUG-log server request rejections #2597

Merged
merged 4 commits into from
Apr 19, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
object ServerCargoDependency {
val AsyncTrait: CargoDependency = CargoDependency("async-trait", CratesIo("0.1"))
val FormUrlEncoded: CargoDependency = CargoDependency("form_urlencoded", CratesIo("1"))
val FuturesUtil: CargoDependency = CargoDependency("futures-util", CratesIo("0.3"))
val Mime: CargoDependency = CargoDependency("mime", CratesIo("0.3"))
val Nom: CargoDependency = CargoDependency("nom", CratesIo("7"))
val OnceCell: CargoDependency = CargoDependency("once_cell", CratesIo("1.13"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
"Cow" to RuntimeType.Cow,
"DateTime" to RuntimeType.dateTime(runtimeConfig),
"FormUrlEncoded" to ServerCargoDependency.FormUrlEncoded.toType(),
"FuturesUtil" to ServerCargoDependency.FuturesUtil.toType(),
"HttpBody" to RuntimeType.HttpBody,
"header_util" to RuntimeType.smithyHttp(runtimeConfig).resolve("header"),
"Hyper" to RuntimeType.Hyper,
Expand Down Expand Up @@ -182,7 +183,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
rustTemplate(
"""
if !#{SmithyHttpServer}::protocols::accept_header_classifier(request.headers(), ${contentType.dq()}) {
return Err(#{RuntimeError}::NotAcceptable)
return Err(#{RequestRejection}::NotAcceptable);
}
""",
*codegenScope,
Expand All @@ -201,9 +202,7 @@ class ServerHttpBoundProtocolTraitImplGenerator(
?.let { "Some(${it.dq()})" } ?: "None"
rustTemplate(
"""
if #{SmithyHttpServer}::protocols::content_type_header_classifier(request.headers(), $expectedRequestContentType).is_err() {
return Err(#{RuntimeError}::UnsupportedMediaType)
}
#{SmithyHttpServer}::protocols::content_type_header_classifier(request.headers(), $expectedRequestContentType)?;
""",
*codegenScope,
)
Expand All @@ -213,9 +212,9 @@ class ServerHttpBoundProtocolTraitImplGenerator(

// Implement `from_request` trait for input types.
val inputFuture = "${inputSymbol.name}Future"
// TODO(https://github.com/awslabs/smithy-rs/issues/2238): Remove the `Pin<Box<dyn Future>>` and replace with thin wrapper around `Collect`.
rustTemplate(
"""
// TODO(https://github.com/awslabs/smithy-rs/issues/2238): Remove the `Pin<Box<dyn Future>>` and replace with thin wrapper around `Collect`.
#{PinProjectLite}::pin_project! {
/// A [`Future`](std::future::Future) aggregating the body bytes of a [`Request`] and constructing the
/// [`${inputSymbol.name}`](#{I}) using modelled bindings.
Expand Down Expand Up @@ -252,13 +251,17 @@ class ServerHttpBoundProtocolTraitImplGenerator(
.await
.map_err(Into::into)
};
use #{FuturesUtil}::future::TryFutureExt;
let fut = fut.map_err(|e: #{RequestRejection}| {
#{Tracing}::debug!(error = %e, "failed to deserialize request");
#{RuntimeError}::from(e)
});
$inputFuture {
inner: Box::pin(fut)
}
}
}

""".trimIndent(),
""",
*codegenScope,
"I" to inputSymbol,
"Marker" to protocol.markerStruct(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub enum ResponseRejection {
pub enum RequestRejection {
#[error("error converting non-streaming body to bytes: {0}")]
BufferHttpBodyBytes(crate::Error),
#[error("request contains invalid value for `Accept` header")]
NotAcceptable,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make these take in the invalid HeaderValue in a follow-up PR.

#[error("expected `Content-Type` header not found: {0}")]
MissingContentType(#[from] MissingContentTypeReason),
#[error("error deserializing request HTTP body as JSON: {0}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ pub enum RequestRejection {
#[error("error converting non-streaming body to bytes: {0}")]
BufferHttpBodyBytes(crate::Error),

/// Used when the request contained an `Accept` header with a MIME type, and the server cannot
/// return a response body adhering to that MIME type.
#[error("request contains invalid value for `Accept` header")]
NotAcceptable,

/// Used when checking the `Content-Type` header.
#[error("expected `Content-Type` header not found: {0}")]
MissingContentType(#[from] MissingContentTypeReason),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl From<RequestRejection> for RuntimeError {
match err {
RequestRejection::MissingContentType(_reason) => Self::UnsupportedMediaType,
RequestRejection::ConstraintViolation(reason) => Self::Validation(reason),
RequestRejection::NotAcceptable => Self::NotAcceptable,
_ => Self::Serialization(crate::Error::new(err)),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum RequestRejection {
#[error("error converting non-streaming body to bytes: {0}")]
BufferHttpBodyBytes(crate::Error),

#[error("request contains invalid value for `Accept` header")]
NotAcceptable,

#[error("expected `Content-Type` header not found: {0}")]
MissingContentType(#[from] MissingContentTypeReason),

Expand Down